jinzhe

jinzhe

github
email

JavaScript Types

Number#

Numbers are divided into integers and floating-point numbers, which can be represented using decimal, octal, hexadecimal, and scientific notation.

var a = 1
var b = 1.1
var c = 067
var d = 0xA
var e = 1.23e7

console.log(a,b,c,d,e)

NaN#

Represents a non-number, and any operation involving NaN will return NaN. NaN is not equal to any value (including itself).

BigInt#

BigInt is used to represent integers of arbitrary length.

The number type cannot safely represent integers greater than 25312^{53}-1 (i.e., 9007199254740991), or less than 2531-2^{53}-1.

Warning

bigInt type is not compatible with number type and cannot be directly calculated.

const bigInt = BigInt(1234567890123456789012345678901234567890);

String#

String types must be enclosed in quotes.

let str = "Hello";  // Double quotes
let str2 = 'Single quotes are ok too';  // Single quotes
let phrase = `can embed another ${str}`;  // Backticks

Boolean#

Only two values: true and false

Null#

Represents nothing, empty, unknown.

Undefined#

Represents not being assigned a value.

var str;
console.log(str) // undefined

Symbol#

Represents a unique identifier.

let user = { 
  name: "zhang"
};
var id = Symbol('id');

user[id] = 1

console.log(user, user[id], user.id)

Hidden properties#

Global Symbol.for#

Get the same symbol by using the same description.

let id = Symbol.for("id"); // Create it if it doesn't exist
let idAgain = Symbol.for("id");
console.log( id === idAgain );

Symbol.keyFor#

Get the description by using the symbol.

Warning

Symbol.keyFor only works for symbol created by Symbol.for.

let id = Symbol.for('id')
let name = Symbol('id')
console.log(Symbol.keyFor(id),Symbol.keyFor(name))
  • Symbol.hasInstance: Called when other objects use instanceof to determine if they are instances of the object.
  • Symbol.isConcatSpeardable: Determines if concat() should be spread.
  • Symbol.species: Allows manual setting of the constructor for derived objects.
  • Symbol.match: Called when match() is invoked.
  • Symbol.search: Called when search() is invoked.
  • Symbol.replace: Called when replace() is invoked.
  • Symbol.split: Called when split() is invoked.
  • Symbol.iterator: Called when iterator methods are invoked.
  • Symbol.toPrimitive: Called when an object is converted to a primitive type.
  • Symbol.toStringTag: Called when toString() is invoked to customize the output.
  • Symbol.unscopables: Excludes properties from being included in a with statement.

Object type#

Stores collections of data and more complex entities.

6 ways to determine types#

typeof#

Can only recognize primitive types and reference types. typeof x and typeof(x) are the same, the parentheses here are not part of typeof. They are parentheses for mathematical grouping.

Note

JavaScript programming language design error, JavaScript converts data to 32-bit storage, the null tag type is the same as object, both are 000 link.

console.log(typeof 1)
console.log(typeof '1')
console.log(typeof undefined)
console.log(typeof true)
console.log(typeof Symbol())
console.log(typeof null)
console.log(typeof [])
console.log(typeof {})
console.log(typeof console)
console.log(typeof console.log)

constructor#

Points to the constructor of the current instance.

let str = 'Covid-19'
console.log(str.constructor)

let number = 123
console.log(number.constructor)

let arr = [1,2,3]
console.log(arr.constructor)

let fun = function(){}
console.log(fun.constructor)

let obj = {}
console.log(obj.constructor)

instanceof#

Checks if it is an instance of the constructor in the prototype chain.

Warning

Primitive types do not have a prototype chain in JavaScript. So the instanceof operator will only return false for primitive types.

let arr = [1,2,3]
console.log(arr instanceof Array) 

let fun = function(){}
console.log(fun instanceof Function)

let obj = {}
console.log(obj instanceof Object)
  
let number = 123;
console.log(number instanceof Number);

let string = '123'
console.log(number instanceof String);

let boolean = true
console.log(number instanceof Boolean);

Object.prototype.toString#

Can determine the data type well, wrap it into a method.

console.log(Object.prototype.toString({}))       
console.log(Object.prototype.toString.call({}))
console.log(Object.prototype.toString.call(1))    
console.log(Object.prototype.toString.call('1'))  
console.log(Object.prototype.toString.call(true))  
console.log(Object.prototype.toString.call(function(){}))  
console.log(Object.prototype.toString.call(null))   
console.log(Object.prototype.toString.call(undefined)) 
console.log(Object.prototype.toString.call(/123/g))    
console.log(Object.prototype.toString.call(new Date())) 
console.log(Object.prototype.toString.call([]))       

Duck typing#

Determine by checking specific properties of itself.

let str = 'Covid-19'
console.log(str.toLowerCase())

let arr = [1,2,3]
console.log(arr.join(','))

Equality comparison#

console.log(null === null)

console.log(undefined === void 0)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.