Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript is embedded in JavaScript


May 09, 2021 CoffeeScript


Table of contents


Embedding JavaScript

Problem

You want to embed found or pre-written JavaScript code in CoffeeScript.

Solution

Wrap JavaScript into an apostrophe:

`function greet(name) {
return "Hello "+name;
}`

# Back to CoffeeScript
greet "Coffee"
# => "Hello Coffee"

Discuss

This is the easiest way to integrate a small amount of JavaScript into The CoffeeScript code without having to convert them with the CoffeeScript syntax. As shown in CoffeeScript Language Reference, you can mix the code for both languages within a certain range:

hello = `function (name) {
return "Hello "+name
}`
hello "Coffee"
# => "Hello Coffee"

The variable "hello" here is still in CoffeeScript, but the function assigned to it is written in JavaScript.