Basic syntax, variable declaration and data type of JavaScript
in Note with 2 comments
Basic syntax, variable declaration and data type of JavaScript
in Note with 2 comments

notes

 //Single line comment /*  This is a multiline comment multiline comment  */ /*···/* Nested comments */···*/

statement

Three statements:

variable

In JavaScript language, an identifier must start with a letter, underscore (_) or dollar ($) symbol; Subsequent characters can contain numbers (0-9). Because the JavaScript language is case sensitive, the letters can be (uppercase) "A" to "Z" and (lowercase) "a" to "z".

Declare variables

There are three ways to declare variables:

Evaluate variables

Attempting to access an uninitialized variable will result in a ReferenceError Exception thrown

 var a; console.log("The value of a is " + a); //  logs "The value of a is undefined" console.log("The value of b is " + b); //  throws ReferenceError exception

You can use undefined to determine whether a variable has been assigned

 var input; if(input === undefined){ doThis(); } else { doThat(); }

The undefined value will be treated as false in the Boolean type environment

 var myArray = new Array(); if (!myArray[0]) myFunction();

The undefined value in the numerical type environment will be converted to NaN (Not a Number)

 var a; a + 2; //  Evaluates to NaN

When evaluating an empty variable, null will be treated as 0 in the numerical type environment, and false in the Boolean type environment

 var n = null; console.log(n * 32); //  logs 0

Field of variable

Variables declared outside all functions are called global variables because they can be accessed by other codes in the current document.

Variables declared inside a function are called local variables because they can only be accessed inside the function.

 if (true) { var x = 5; } console.log(x); //  five

Use let declaration

 if (true) { let y = 5; } console.log(y); //  ReferenceError: y is not defined

Variable declaration promotion

Referencing variables declared later without throwing exceptions is called variable declaration lifting

The promoted variable will return undefined value

 /** * Example 1 */ console.log(x === undefined); //  logs "true" var x = 3; /** * Example 2 */ // will return a value of undefined var myvar = "my value"; (function() { console.log(myvar); //  undefined var myvar = "local value"; })();

Can also write

 /** * Example 1 */ var x; console.log(x === undefined); //  logs "true" x = 3; /** * Example 2 */ var myvar = "my value"; (function() { var myvar; console.log(myvar); //  undefined myvar = "local value"; })();

Suggestion: All var statements in a function should be placed as close to the top of the function as possible to improve the clarity of the code

constant

The naming rule of constant identifiers is the same as that of variables: they must start with a letter, underscore, or dollar sign and can contain letters, numbers, or underscores.

Created with the keyword const, read-only and cannot be modified

 const prefix = '212';

Constants cannot be changed by assignment, nor can they be redeclared when the script is running. It must be initialized to a value

The scope rules of constants are the same as let block level scope variables

Constants cannot be named with the same name as variables or functions in the same scope

 // THIS WILL CAUSE AN ERROR function f() {}; const f = 5; // THIS WILL CAUSE AN ERROR ALSO function f() { const g = 5; var g; //statements }

Data structure and type

7 kinds in total

Conversion of data types

It is not necessary to specify the data type when declaring variables, and the data type will be automatically converted when the script execution requires

 var answer = 42;

You can also assign a string value to the same variable

 answer = "Thanks for all the fish...";

In numeric and string expressions involving the addition operator (+), JavaScript will convert numeric values into strings

 x = "The answer is " + 42 // "The answer is 42" y = 42 + " is the answer" // "42 is the answer"

When other operators are involved, such as - , the JavaScript language does not change numbers into characters

The first example is a mathematical operation, and the second example is still a string operation

 "37" - 7 // 30 "37" + 7 // "377"

Convert string to number

ParseInt() and parseFloat()

ParseInt can only return integers, so using it will lose the decimal part. When calling parseInt, it is best to always take the radix parameter, which is used to specify which number system to use

 parseInt(string, radix);

The following examples all return 15

 parseInt(" 0xF", 16); parseInt(" F", 16); parseInt("17", 8); parseInt(021, 8); parseInt("015", 10); parseInt(15.99, 10); parseInt("15,123", 10); parseInt("FXX123", 16); parseInt("1111", 2); parseInt("15*3", 10); parseInt("15e2", 10); parseInt("15px", 10); parseInt("12", 13);

The following examples all return NaN

 parseInt("Hello", 8); //  Not a number at all parseInt("546", 2);   //  Digits are not valid for binary representations

The parseFloat() method parses the string specified in the parameter into a floating point number and returns

 parseFloat(string)

The following examples all return 3.14

 parseFloat("3.14"); parseFloat("314e-2"); parseFloat("0.0314E+2"); parseFloat("3.14more non-digit characters");

The following example returns NaN

 parseFloat("FF2");
Monocular addition operator
 "1.1" + "1.1" = "1.11.1" (+"1.1") + (+"1.1") = 2.2   // Note: the parentheses are added for clarity, not required.

Reading materials: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide

Review method: practice (write code once, run once, test once), Google where you don't understand, read and take notes

Bottom line principle: prefer rewriting once rather than copying and pasting

This review includes: notes, declarations, constants, data structures and types

Review time: about 2 hours... I don't know why it takes so long···

Responses
  1. Jack Chen

    sure. 😆

    Reply
    1. @Jack Chen

      thank you. 😀 😄

      Reply