snippet Saturday 20 April 2024

JavaScript Functions. New, old and everything in between

Author

// function declarations
function myFunc1(){
  console.log('hello')
}
myFunc1()

// function expressions
const myFunc2 = function(){
  console.log('goodbye')
}
myFunc2();

// iife
(function(){
  console.log('good day')
})()

// es6 arrow function
const myFunc3 = () => {
  console.log('top of the morning to ya')
}
myFunc3()

// es6 arrow function - only one line can be returned without {}
const myFunc4 = () => console.log('good night')
myFunc4()

// es6 arrow function - if only one parameter you can omit the ()
const myFunc5 = name => console.log('good night ' + name)
myFunc5('Ana')

// es6 arrow function - if two parameters you can not omit the ()
const myFunc6 = (name,place) => console.log(name + ' is going to the ' + place)
myFunc6('Ana', 'moives')

Code playground