close
close
coffeescript cheat sheet

coffeescript cheat sheet

2 min read 19-10-2024
coffeescript cheat sheet

CoffeeScript Cheat Sheet: A Quick Guide to Concise JavaScript

CoffeeScript, a language that compiles into JavaScript, offers a more concise and readable syntax, making it a popular choice for web developers. This cheat sheet will guide you through the essential features of CoffeeScript, enabling you to write cleaner and more expressive code.

1. Basic Syntax

  • No Semicolons: CoffeeScript automatically inserts semicolons at the end of each statement, simplifying your code.
  • Indentation Matters: CoffeeScript uses indentation to define blocks of code, eliminating the need for curly braces.
  • Variables: Variables are declared using var, but you can omit it if the variable is assigned within a function scope.
  • Functions: Functions are defined using -> instead of function.
  • Comments: Comments start with #.

Example:

# A simple CoffeeScript function
add = (a, b) -> a + b

# Calling the function
sum = add 2, 3

# Outputting the result
console.log sum

2. Control Flow

  • If/Else: if, else if, and else statements are straightforward.
if age < 18
  console.log "You are a minor"
else if age >= 18 and age < 65
  console.log "You are an adult"
else
  console.log "You are a senior"
  • Loops: for and while loops work similarly to JavaScript.
# Looping through an array
for number in [1, 2, 3, 4]
  console.log number

# Looping while a condition is true
i = 0
while i < 10
  console.log i
  i++
  • Switch/Case: The switch statement provides a concise way to handle multiple conditions.
day = "Tuesday"

switch day
  when "Monday"
    console.log "Start of the week"
  when "Friday"
    console.log "End of the week"
  else
    console.log "Another day"

3. Data Structures

  • Arrays: Use square brackets [] to create arrays.
numbers = [1, 2, 3]

# Accessing elements
console.log numbers[0] # Output: 1
  • Objects: Use curly braces {} to create objects.
user = 
  name: "John"
  age: 30

# Accessing properties
console.log user.name # Output: John
  • Lists: CoffeeScript offers a compact way to define lists.
names = "Alice", "Bob", "Charlie"

# Accessing elements
console.log names[1] # Output: Bob

4. Useful Features

  • Spreads: Use ... to expand an array or object into multiple arguments.
numbers = [1, 2, 3]
console.log Math.max ...numbers # Output: 3
  • Comprehensions: Use comprehensions for concise iteration and data manipulation.
squares = (x*x for x in [1, 2, 3]) # Output: [1, 4, 9]
  • Classes: CoffeeScript simplifies defining classes.
class Person
  constructor: (@name, @age) ->
  greet: ->
    console.log "Hello, my name is #{@name}"

person = new Person "John", 30
person.greet() # Output: Hello, my name is John

5. Code Optimization

CoffeeScript excels at writing concise and efficient code, making it a great choice for optimizing your JavaScript. Here are some key points:

  • Variable Hoisting: CoffeeScript automatically hoists variables to the top of their scope, eliminating potential confusion.
  • Implicit Returns: Functions implicitly return the last evaluated expression, reducing boilerplate code.
  • Built-in Functions: CoffeeScript includes various built-in functions for common tasks, such as map, filter, and reduce.

Resources:

Conclusion:

This cheat sheet provides a starting point for mastering CoffeeScript. The language's concise syntax, elegant features, and efficient code generation make it a valuable tool for web development. By understanding the fundamentals of CoffeeScript, you can write cleaner and more expressive JavaScript code.

Note: This cheat sheet is based on the information available in the CoffeeScript documentation and other online resources. Please refer to official documentation for the most up-to-date information and advanced features.

Related Posts


Latest Posts