Look at the code and think about the output

var scope = 'global';
function f(){
    console.log(scope);
    var scope = 'local';
    console.log(scope);
}
f();

var a = [];
console.log(a == 0,a == false);

var b = [6];
console.log(b + 1,b-1);

var c = {};
console.log(Boolean(c)==true);Copy the code

Compared to the answer



If you answered all the questions correctly, you don’t need to read the following. I believe you have already laid a solid foundation

Don’t be discouraged if you don’t get it right, this article will consolidate your basic knowledge, and there will be a series of basic review knowledge to enjoy you!

The reason

I believe that as long as the students engaged in development, regardless of the front end or back end more or less used javascript this language. However, I do not believe that most of them have a deep understanding of it. I have read many articles shared by students, but to be honest, there are few articles with very little knowledge and my own opinions. I also rarely see any article about the basic knowledge of javascript, which says that a great building starts from the ground, and the importance of foundation is no less than that of front-end framework. Mainly from JS authoritative guide, network information and some of their own “prejudice”.

The data type

JavaScript has two main types: primitive types and object types

Primitive type

null

undefined

string

number

boolean

Five primitive types: Boolean members are only true and false. Number is a numeric value. String is a string (16 bits long). You can’t change the string h, and you can’t change false to anything else. Only the object type is mutable

Object Type

An object type is a collection of properties. Most objects contain property names, property values (array objects can be viewed as ordered property names starting at 0), that is, name/value pairs, but a function is a special object

Common object types:

Array, Function, Math, Date, RegExp

Type conversion

value

Convert to string

digital

Boolean value

object

undefined “undefined” NaN false throws TypeError
null “null” 0 false throws TypeError
true “true” 1 new Boolean(true)
false “false” 0 new Boolean(false)
“” 0 false New String (” “)
“1.1” 1.1 true 1.1 new String (” “)
“ccy” NaN true New String (” ccy “)
0 “0” false new Number(0)
0 “0” false new Number(-0)
NaN “NaN” false new Number(NaN)
Infinity “Infinity “ true new Number(Infinity)
-Infinity “-Infinity ” true new Number(-Infinity)
1 “1” true new Number(1)
{} Object first converts the original value,

Then the original value is further transformed
With the left true
[] 0 true
[6] “6” 6 true
[‘a’] Use the join() method NaN true
function(){} “function(){}” NaN true

JavaScript value types are very flexible. When you want to use a Boolean value, you can provide any type of value. JavaScript converts primitives to object types as needed, including strings and numbers. Understand type conversions, and the output of a, B, and C at the beginning of this article should not be a problem.

Variable declarations

ES5 era JavaScript did not support block-level scope and declared variables using the keyword var

As follows:

var i; var ccy,name; Var m = 0, k = 1, n = 0'bar';Copy the code

If no initial value is assigned to a variable declared by var, it defaults to undefined and can be of any data type.

Variable scope

The scope of a variable is the area in the program source code where it is defined. Global variables have global effects and are defined anywhere in JavaScript. However, variables declared within a function are only valid within the body of the function, that is, local variables. Function parameters are also local variables.

Those of you who have studied Java should know that the proximity principle means that a class variable with the same name as a variable in a method is overwritten in the body of the method or constructor. JavaScript also follows this principle.

The var keyword has an early declaration bug and will not report an error in non-strict mode. The output of the method at the beginning of this article is equivalent to

function f(){
    var scope;
    console.log(scope);
    var scope = 'local';
    console.log(scope);
}Copy the code

Understand variable declarations and scopes, as well as the output at the beginning of this article.

The scope chain

JavaScript is a lexical scoped language. Global variables are always defined in a program, and local variables are always defined in declared function questions and nested functions.

Each piece of JavaScript code has a scope chain associated with it. The scope chain is a list or linked list of objects that define the variables in the “scope” of the code. When it needs to find the value of variable X (variable resolution), it starts with the first object in the chain. If there is, it will be directly used; if there is not, it will look up, and so on. If there is no X in the scope chain, it will throw ReferenceError exception.

I hope this article can harvest for you, wish you all the best!