Basic knowledge of JavaScript-variables, data types

Basic knowledge of JavaScript-variables, data types

What is JavaScript?

JavaScript is a literal scripting language, a dynamically typed, weakly typed, prototype-based language. Popularity: JS is the most important part of the front-end code (behavior layer). It is commonly used to manipulate HTML pages , respond to user operations , and verify data transmission .

Declare variable

Use letor vardeclare variables, which can be used to store various types of data for subsequent use.

Variable naming rules

Strictly case sensitive;

Can only contain alphanumeric _ $ , can not start with a number;

Cannot use keywords/reserved words;

Unless needed, do not overwrite the existing API;

See the name knows the meaning.

The difference between var and let:

**var:** When the page loads, there are two processes: 1. First define the stage

Will find the variables defined by var in the current scope (only define variables, not assign values ​​in advance)

2. Execution stage

Start from the beginning and execute the code down

1) It is not regarded as a scope in if, for, while···. Variables defined in these are equal to global variables, but only as a scope in the function() function.

console.log(a);  //undefined

if(false){
var a = 10;
}

console.log(a);  //undefined
a:是全局变量

2) When defining the same variable, the latter will overwrite the former.

**let: **1) Not only the function is regarded as a scope, but also as a scope within if, for, ···, the variables defined are not global variables

2) Undefined variables cannot be used in advance

console.log(a);

let a = 10;

3) The same scope cannot let the same variable

let a = 1;

let a = 2;

type of data

The data types are divided into:

Basic data types: number, string, boolean, undefined, null

Complex data types: reference data types

Basic data type

number type

Number types include integers, decimals (floating point numbers)

Special: NAN is a number type

let a = 10;

let b = 1.1;
string string type

String types are enclosed in single quotes, double quotes, and template string brackets.

The brackets can be Chinese, English, numbers, and symbols, but the single quotes, double quotes, and template strings that define the string type cannot be nested. If you must add them, you need to use escape symbols.

let a = "中文";

let b = 'english';

let c = `111`;

let d = "中"国"";  //这种写法错误
let f = "中\"国\"";  //正确写法
boolean Boolean type

The Boolean type has only true and false: true and false;

let a = false;

let b = true;
undefined undefined type

An undefined type is just a declaration of unassigned. Not pointing to anything

let a ;

let b = undefined;
null pointer

I don't know what the value of the variable is at the beginning, but it is certain that the variable is stored as an object type

let a = null;

Reference data type

Object type
Array

An array is a piece of data, which is used to store a group of data

let a = [];//定义
let b = [100,"数组",1,true];
//取数据,数组里面的数都有一个下标,利用下标取数据,从0开始
console.log( b[2] );   //结果是1
console.log( b.length );  //取长度,为4
//给某一项重新赋值
a[0] = 20;  
Object object data

Object: the description of a thing

Two attribute names cannot be the same, the same will be overwritten;

The data in the object has no order.

let a = {
//(属性)键  : 值
		name  :  "姓名",
		age :  20,
		height  :  "175cm",
 };
 //取值
 console.log(a.name);  //方法一
 console.log(a["name"]);   //方法二,[]里面表示一个数据,没有加“”号表示是一个变量
 //修改某个属性
a.age = 22;
//删除某个属性
delete a.age;
//添加新属性
a.tel = "12345678901";

Same and different with array:

Same: can store different data; can store multiple data

Different: There is a description for each data, called attribute

Nested value
let a = {
//(属性)键  : 值
		name  :  "姓名",
		age :  20,
		height  :  "175cm"
		friend:{
			name : "名字",
			hobby : ["唱歌","画画","弹琴"]
		}
 };
 //取数据
 console.log( a.friend.hobby[2]);  //弹琴
function function

Encapsulate a bunch of code so that it will not be called immediately after the browser is run, and will be executed unless the function is called

function a (){
    //定义一个函数,a为函数名
 } 
 //函数调用
 a();
Built-in object node object

It has been defined in JavaScript in advance and can be obtained without definition.

For example: window, document

typeof operator

Used to detect the type of basic data, the return is a string

console.log( typeof 10 );     //number
console.log( typeof "10" );   //string
console.log( typeof true );    //boolean
console.log( typeof undefined );  //undefined
console.log( typeof null );  //特殊:返回的是object
console.log( typeof [] );    // object
console.log( typeof {} );    //object
console.log( typeof function a(){} );   //返回function,但这个是函数,属于的是object类型
The difference between value type and reference data type

Value type

Value type refers to the value of primitive type, also called basic type.

Data type: number, string, boolean, null, undefined

Stored in the stack, occupies a fixed memory space, and is destroyed after use

Assignment method: 1) Copy the value and create a new object

​ 2) Save and copy are the value itself

​ 3) The two pieces of data are completely independent in memory

Immutable value

The scope is in the scope of the function. It takes effect when the function is modified inside, and it becomes invalid when the function is destroyed.

The comparison of objects and objects is: comparison of values

Type detection: typeof operator

Reference type

The reference value is the value of the guiding type.

Data type: object, function, array, data, regexp

Stored in the heap, it occupies an unstable memory space, and may not be destroyed after use. Only when an object has no references, the system's garbage collection mechanism will recycle and destroy it.

Assignment method: 1) Copy of reference, create a new reference

​ 2) Save and copy is a pointer to the object

​ 3) The object constructed using the new() method is a reference type

Variable value

The scope is the value in the runtime data area that is modified when the function is modified. Even if the function is destroyed, the value of the variable is still changed.

The comparison of objects and objects is: comparison of addresses

Type detection: instanceof operator

Guess you like

Origin blog.csdn.net/leilei__66/article/details/106963986