Write concise source expressions and understand their JavaScript compilation.
Before starting: Basic JavaScript values, functions and Node.js.
Practice project: Extend the examples into a small CoffeeScript study demo. Explain the setup, one normal case and one failure or boundary case.
Start here: concepts and guided practice
- CoffeeScript: a small function
CoffeeScript compiles into JavaScript. The thin arrow defines a function; its final expression supplies the result. Read the generated JavaScript when debugging.
Node.js and CoffeeScript 2 installed in a practice project; save as study.coffee and use the installed coffee command.
double = (value) -> value * 2 console.log double(6)Check the output
12Change the input to -2 and predict -4.
- CoffeeScript: transform a collection
A comprehension visits collection entries and builds a result array. This example keeps the source array and creates a new array of doubled scores.
Node.js and CoffeeScript 2 installed in a practice project; save as study.coffee and use the installed coffee command.
scores = [3, 5, 8] doubled = (score * 2 for score in scores) console.log doubled.join(", ")Check the output
6, 10, 16Use an empty array and explain the empty printed line.
- CoffeeScript: make a condition return a value
In CoffeeScript, an if expression can supply a value. Use an explicit boundary and keep the two result labels easy to understand.
Node.js and CoffeeScript 2 installed in a practice project; save as study.coffee and use the installed coffee command.
score = 60 label = if score >= 60 then "Pass" else "Practice" console.log labelCheck the output
PassCheck scores 59 and 60.
Open the CoffeeScript learning path on DevStudyHub · Play coding games on this blog
No comments:
Post a Comment
Please do not enter any spam link in the comment box.