Home  >  Article  >  Web Front-end  >  What data types does ECMAScript have?

What data types does ECMAScript have?

青灯夜游
青灯夜游Original
2022-01-27 14:06:112281browse

ECMAScript data types are divided into two types: 1. Basic data types, including String, Number, Boolean, undefined, null and Symbol types; 2. Reference data types, including Object, Function and Array types.

What data types does ECMAScript have?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

ECMAScript data types can be divided into two types: basic data types and reference data types

Basic types

Basic types are also called value types.

  • String: Any string

  • Number: Any number

  • Boolean: true, false

  • ## undefined: undefined

  • null: null

  • Symbol

##Object TypeObject type is also called reference type

    Object: any object
  • Function: a special Object (difference from Object: can be executed)
  • Array: a special object (numeric subscript, internal data is ordered)

How to determine the type of datatypeof: can determine undefined, numerical value, string, Boolean value, function

cannot determine : null and Object, Object and Array

Returns the string expression of the data type.

instanceof: Determine the specific type of the object.
===: Can judge undefined and null


1. Basic type judgment method:

var a;
console.log(a);//undefined
console.log(typeof a);//"undefined"
console.log(a===undefined);//true
a=4;
console.log(typeof a==="number");//true
a='dewferf';
console.log(typeof a==='string');//true
console.log(typeof a==='String');//false
a=true;
console.log(typeof a === 'boolean');//true
a=null;
console.log(typeof a,a===null);//"object",true

2. Reference type Judgment method

var b1={
	b2:[1,'avc',console.log],
	b3:function(){
	console.log('b3');
	return function(){
		return 'lxyxxx';
	}
	}	
};
console.log(typeof b1.b2);//'object'
console.log(b1 instanceof Object,b1 instanceof Array);//true,false
console.log(b1.b2 instanceof Array,b1.b2 instanceof Object);//true,true
console.log(b1.b3 instanceof Function,b1.b3 instanceof Object);//true,true

console.log(typeof b1.b3);//'function'
console.log(typeof b1.b3 === 'function');//true

console.log(typeof b1.b2[2]);//'function'
console.log(typeof b1.b2[2] === 'function');//true
b1.b2[2](4);//因为b1.b2[2]是函数,所以会执行
b1.b3()();//

Symbol type


Symbol is a new data type introduced in ECMAScript6, which represents a unique value. Symbol type The value needs to be generated using the Symbol() function, as shown in the following example:

var str = "123";
var sym1 = Symbol(str);
var sym2 = Symbol(str);
console.log(sym1);          // 输出 Symbol(123)
console.log(sym2);          // 输出 Symbol(123)
console.log(sym1 == sym2);  // 输出 false :虽然 sym1 与 sym2 看起来是相同的,但实际上它们并不一样,根据 Symbol 类型的特点,sym1 和 sym2 都是独一无二的

The parameters of the Symbol function only represent the description of the current Symbol value, so the return values ​​​​of the Symbol function with the same parameters are not equal. of.


#The new command cannot be used before the Symbol function, otherwise an error will be reported. This is because the generated Symbol is a primitive type value, not an object. That is,
Since Symbol values ​​are not objects, properties cannot be added. Basically, it is a string-like data type.


[Related recommendations:
javascript learning tutorial

]

The above is the detailed content of What data types does ECMAScript have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn