0% found this document useful (0 votes)
2 views

JavaScript

The document discusses various ways to define functions in JavaScript, including function declarations, anonymous functions, and arrow functions. It highlights the concept of hoisting, which allows function declarations to be called before they are defined, unlike function expressions. Additionally, it contrasts traditional function syntax with arrow function syntax, noting that arrow functions do not use the 'function' keyword.

Uploaded by

Lucas Fernandes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript

The document discusses various ways to define functions in JavaScript, including function declarations, anonymous functions, and arrow functions. It highlights the concept of hoisting, which allows function declarations to be called before they are defined, unlike function expressions. Additionally, it contrasts traditional function syntax with arrow function syntax, noting that arrow functions do not use the 'function' keyword.

Uploaded by

Lucas Fernandes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JavaScript

Different ways to define a function on JavaScript


Here we're going to talk a little bit about different ways to declare a function and JavaScript

Function Declaration:

function add (num1, num1){


return num1 + num2
}
console.log(add(1,2))

Anonimous function:

let add = function (num1,num2){


return num1 + num2
}
console.log(add(3,4))

The difference between function declaraton and


function expressions:
The two functions run the same way except with one edge case "Hoisting"

Because JS reads all the script before runs the script we can call a function before declare it
using function declaration (Hoisting)
"It already knows that I have a declared function after I call it"

When we use function expressions (functions assigned to a variable)


Because JS knows that there's a variable, but it doesn't know if it's a function or a string and it
doesn't know what is the value of that variable;

Structuring a code in JavaScript

// Variables
let name = "lucas"
// Methods & Functions
function callName () {
console.log(name)
}
// Inits & Event Listeners
callName()

Arrow Functions:

// a traditional function
let addTraditional = function (num1, num2){
num1 + num2;
console.log(add(3,4))
}

// an arrow function version


let add = (num1, num2) => {
return num1 + num2;
}
console.log(add(3,4))

The difference between a expression function and an


arrow function:
In arrow function there's no use of word function

You might also like