Chapter three Language foundation

This is the 11th day of my participation in the August More Text Challenge

This chapter is the cornerstone of the JavaScript language. It is very, very important, and many new scholars do not systematically learn this knowledge. When following the video study on the website is not to teach so detailed, but also to those self-taught brothers to make a supplement. After all, some knowledge does not encounter nothing, encountered will not be a barrier.

3.7 the function

Functions are a core component of any language because they can encapsulate statements and execute them anywhere, at any time. Functions in ECMAScript are declared using the function keyword, followed by a set of parameters, followed by the function body.

Note that Chapter 10 covers functions in more detail.

Here is the basic syntax for functions:

function functionName(arg0, arg1,... Function sayHi(name, message) {console.log("Hello "+ name + "," + message); function sayHi(name, message) {console.log("Hello "+ name + "," + message); }Copy the code

A function can be called by its name, and the arguments to be passed to the function are enclosed in parentheses (separated by commas if there are more than one). Here is an example of calling the function sayHi() :

sayHi("Nicholas", "how are you today?" );Copy the code

Calling this function yields “Hello Nicholas, how are you today?” . The parameters name and message are concatenated together as strings inside the function and are eventually printed to the console via console.log.

Functions in ECMAScript do not need to specify whether to return a value. Any function can use a return statement to return the value of the function at any time, followed by the value to be returned. Such as:

function sum(num1, num2) { 
 return num1 + num2; 
} 
Copy the code

The function sum() adds the two values and returns the result. Note that there is no special declaration that the function has a return value other than the return statement. It can then be called like this:

const result = sum(5, 10); 
Copy the code

Note that the function stops execution and exits as soon as a return statement is encountered. Therefore, the code following the return statement is not executed. Such as:

function sum(num1, num2) { return num1 + num2; console.log("Hello world"); // do not execute}Copy the code

In this case, console.log is not executed because it comes after the return statement. A function can also have multiple return statements, like this:

function diff(num1, num2) { if (num1 < num2) { return num2 - num1; } else { return num1 - num2; }}Copy the code

The diff() function is used to calculate the difference between two values. If the first value is less than the second, subtract the first from the second; If not, subtract the second from the first. Each branch of the code has its own return statement that returns the correct difference.

A return statement can also have no return value. At this point, the function immediately stops execution and returns undefined. This is most often used to prematurely terminate function execution, not to return a value. For example, in the following example, console.log does not execute:

function sayHi(name, message) { return; console.log("Hello " + name + ", " + message); // do not execute}Copy the code

Note that the best practice is for a function to either return a value or not. Functions that return values only under certain conditions can be troublesome, especially when debugging.

Strict mode also has some restrictions on functions:

  • Functions cannot be named eval or arguments;
  • Function arguments cannot be called eval or arguments;
  • Two named parameters cannot have the same name.

If you violate the above rules, syntax errors will result and the code will not execute.

Chapter three summary

The core language features of JavaScript are defined in ECMA-262 in the form of the pseudo-language ECMAScript. ECMAScript contains all the basic syntax, operators, data types, and objects that perform basic computational tasks, but does not provide a mechanism for getting input and producing output. Understanding ECMAScript and its complex details is key to fully understanding JavaScript in a browser. Here’s a summary of the basic elements in ECMAScript.

  • Basic data types in ECMAScript include Undefined, Null, Boolean, Number, String, and Symbol.
  • Unlike other languages, ECMAScript does not distinguish between integer and floating point values, and has only one numeric data type, Number.
  • Object is a complex data type that is the base class for all objects in the language.
  • Strict patterns impose restrictions on certain error-prone parts of the language.
  • ECMAScript provides many of the basic operators found in C and C-like languages, including mathematical, Boolean, relational, equality, and assignment operators.
  • The flow control statements in this language are mostly borrowed from other languages, such as if statements, for statements, and switch statements.

Functions in ECMAScript are different from functions in other languages.

  • There is no need to specify the return value of a function, because any function can return any value at any time.
  • Functions that do not specify a return value actually return the special value undefined.