Determine the type of variable
typeof
operator. Used to determine whether a variable is a primitive type or an object type.1
console.log(typeof(a))
instanceof
operator: Mainly used to detect whether an object is an instance of a certain constructor.1
2(new Array()) instanceof Array // true
(new Date()) instanceof Date // trueconstructor
property: You can determine the type of an object by accessing itsconstructor
property.1
2(new Array()) instanceof Array // true
(new Date()) instanceof Date // trueNull and Undefined checks: You can compare directly with
null
andundefined
to determine whether a variable isnull
orundefined
.1
2if (variable === null) { /* ... */ }
if (typeof variable === "undefined") { /* ... */ }User-defined type check
In some cases, it may be necessary to implement your own type checking logic, especially if you are using custom objects or classes.
if else statement
1
2
3
4
5
6
7
8
9
10
11
12
13if (condition) {
// Code to be executed when the condition is true
} else {
// Code to be executed when the condition is false
}
if (condition 1) {
//Code to be executed when condition 1 is true
} else if (condition 2) {
// Code executed when condition 1 is false but condition 2 is true
} else {
// Code executed when condition 1 and condition 2 are both false
}If the conditions of multiple
if...else if...else
statements are true, only the code block with the first true condition will be executed.You can use as many
else if
statements as you like, but there can be only oneelse
statement, and it should always be last.
Hoisting of variables
When you use var to declare variables in JavaScript (especially in ES5 and earlier), you encounter a phenomenon called hoisting.
Variable hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope.
Consider the following code:
1
2
3console.log(myVar); // Output: undefined
var myVar = 5;
console.log(myVar); // Output: 5Although the variable
myVar
is declared and initialized both before and after it is referenced, due to variable hoisting, the above code is actually interpreted as:1
2
3
4var myVar; // promoted variable declaration
console.log(myVar); // Output: undefined
myVar = 5; // initialization
console.log(myVar); // Output: 5Note that only the declaration is hoisted, the initialization remains in place.
Hoisting of functions
Function declarations are also hoisted, but this behaves slightly differently than variable hoisting. Both the function’s declaration and its body are hoisted. For example:
1
2
3
4console.log(foo()); // Output: Hello!
function foo() {
return "Hello!";
}However, if you create a function using variable assignment, then only the declaration will be hoisted, not the assignment:
1
2
3
4
5console.log(foo); // Output: undefined
console.log(foo()); // TypeError: foo is not a function
var foo = function() {
return "Hello!";
};To avoid potential confusion and errors caused by hoisting, it is recommended to always declare a variable or function before using it.
let and const
let
andconst
introduced after ES6 (ES2015) also have hoisting behavior, but unlikevar
they are hoisted to the top of the block rather than the top of the function or global context. However, accessing these variables before their hoisted declaration will result in aReferenceError
because they have not actually been initialized. This zone is called the Temporal Dead Zone (TDZ).1
2console.log(a); // ReferenceError
let a = 5;
Multiple Declarations
If you declare the same variable multiple times in the same scope,
var
will not cause a problem, but usinglet
orconst
to declare the same variable multiple times will cause an error.1
2
3
4
5var x = 5;
var x = 10; // no problem
let y = 5;
let y = 10; // SyntaxError
Hoisting is performed during the compilation phase
Before JavaScript code is executed, it goes through the compilation phase, and promotion is completed at this stage. In this process, the JS engine first reads and promotes all declarations before executing the code.