JS functions are values in themselves, and can be used accordingly.
function inchToCm(inches) {
const cm = inches * 2.54;
return cm;
};
Storing an anonymous function as value in a variable.
const inchtoCm = function(inches) {
return inches * 2.54;
};
const inchtoCm = (inches) => {
return inches * 2.54;
};
const inchtoCm = (inches) => inches * 2.54;
If you have only a single parameter, you can remove the parenthesis.
const inchtoCm = inches => inches * 2.54;
A way to run an anonymous function immediatelly when it is created.
(function(){
console.log('Running this Function');
}) ();
function makeABaby(first, last){
const baby = {
name: `${first} ${last}`,
age: 0
}
return baby;
}
const makeABaby = (first, last) => {
return {
name: `${first} ${last}`,
age: 0
}
}
const makeABaby = (first, last) => ({ name: `${first} ${last}`, age: 0 });
A method is simply a function that lives insite of an object. console.log(): log is a function that li
Something that will happen when something is done, such as clicking a button.
function handleclick(){
console.log('Message');
}
button.addEventListener('click', handleClick)
button.addEventListener('click', function(){
console.log('Message');
})
or after a timeout: the below will run functionName after 1000 miliseconds (one second). You can do it as above by either calling a function you declared beforehand
setTimeOut(functionName, 1000);
Or declaring it directly within setTimeOut as an anonymous function
setTimeOut(function(){
console.log('Message');
}, 1000);
Or an arrow function
setTimeOut(()=> { console.log('Message') }, 100));
Hoisting - A fuction declared with the function keyword, is hoisted by JS to the top of the file, so it doesn't care about the order of the document. You can call it even before it's declaration.
Curly Brackets {} mean either a block of code or the creation of an object
Parenthesis () always run first in JS.