function-basis
In JavaScript there is two different standards for the codes. One is the non-strict mod, and another is the strict mode. The strict mod limits the scope of a variable or a function to the block they nest in, while the non-strict mode doesn’t.
Why should there be two different mod?
Because the non-strict mode is the initial style when JS is newly created. And upon it is widely used, the problem of non-strict style imerged. So developments need to change it, but there are still so many old websites using it, if the non-strict mode is totally abandoned, the old websites will no longer work.
So it comes as an optional mode.
We could use the strict mode in node.js by adding a 'use strict';
line in the js file’s at start.
Function Expression
Read the article.
- functions are values. They can be assigned, copied or declared in any place of the code.
- if the function is declared as a separate statement in the main code flow, that’s called a “Function Declaration”
- if the function is created as a part of an expression, it’s called a “Function Expression”
- Function Declarations are processed before the code block is executed. They are visible everywhere in the block.
- Function Expressions are created when the execution flow reaches them.
Arrow Function
Read the article.
Arrow functions are handy for simple actions, especially for one-liners. They come in two flavors:
Without curly braces: (…args) => expression – the right side is an expression: the function evaluates it and returns the result. Parentheses can be omitted, if there’s only a single argument, e.g. n => n*2.
With curly braces: (…args) => { body } – brackets allow us to write multiple statements inside the function, but we need an explicit return to return something.
eg: Replace Function Expressions with arrow functions in the code below:
1 | function ask(question, yes, no) { |
Let’s change it to arrow function expressions:
1 | ask = (question, yes, no) => { |
JavaScript Call Stack
JavaScript engine uses a call stack to manage execution contexts.
The call stack uses the stack data structure that works based on the LIFO (last-in-first out) prinsiple.