cyber-s / blog

tipsや質問などをここにknowledgeとして残します。
0 stars 0 forks source link

🤓 e-Learning Journey - JavaScript #698

Open limincs opened 4 years ago

limincs commented 4 years ago

Functions

Function Declarations

A function declaration binds a function to an identifier. image A function declaration consists of:

Note: We should also be aware of the hoisting feature which allows access to function declarations before they are defined. Example:

console.log(greetWorld()); // Output: Hello, World!

function greetWorld() {
  console.log('Hello, World!');
}

Hoisting allowed greetWorld() to be called before the greetWorld() function was defined. More info

Parameters

Parameters are used as variables inside the function body. When the function is called, these parameters will have the value of whatever is passed in as arguments. image

Arguments

The values that are passed to the function when it is called are called arguments. Arguments can be passed to the function as values or variables. image The order in which arguments are passed and assigned follows the order that the parameters are declared. image

Default Parameters

Default parameters allow parameters to have a predetermined value in case there is no argument passed into the function or if the argument is undefined when called. Example:

function greeting (name = 'stranger') {
  console.log(`Hello, ${name}!`)
}

greeting('Nick') // Output: Hello, Nick!
greeting() // Output: Hello, stranger!

Return

To return a value from a function, we use a return statement.

When a function is called, the default the result of calling a function is undefined. The return statement stops the execution of a function and returns a value from that function.

Helper Functions

We can also use the return value of a function inside another function. Example:

function multiplyByNineFifths(number) {
  return number * (9/5);
};

function getFahrenheit(celsius) {
  return multiplyByNineFifths(celsius) + 32;
};

getFahrenheit(15); // Returns 59

We can use functions to section off small bits of logic or tasks.

Function Expressions

Function expressions create functions inside an expression instead of as a function declaration. They can be anonymous and/or assigned to a variable. In a function expression, the function name is usually omitted (anonymous function). A function expression is often stored in a variable in order to refer to it. image

// Named function
function rocketToMars() {
  return 'BOOM!';
}

// Anonymous function
const rocketToMars = function() {
  return 'BOOM!';
}

Note: Unlike function declarations, function expressions are not hoisted so they cannot be called before they are defined.

Arrow Functions

Arrow functions removes the need to type function keyword. First, include the parameters inside the ( ), then add arrow => that points to the function body surrounded in { }. image Example:

const rectangleArea = function(width, height) {
  let area = width * height;
  return area;
};

becomes:

const rectangleArea = (width, height) => {
  let area = width * height;
  return area;
};

Concise Body Arrow Functions

image A function body composed of a single-line block does not need curly braces. Without the curly braces, whatever that line evaluates will be automatically returned. The contents of the block should immediately follow the arrow => and the return keyword can be removed. This is referred to as implicit return. image Example:

const squareNum = (num) => {
  return num * num;
};

becomes:

const squareNum = num => num * num;
justcallmehide commented 4 years ago

@limincs

good note😃 it's easy to understand.

There's a lot Concise in ES6😂

{} these are called "curly braces" in English. I didn't know that.