JavaScript data types and data type conversion

Disclaimer: This article is a blogger original article, please indicate the source, only for learning exchange and resource sharing. Micro-channel public number: Geeks lamps https://blog.csdn.net/zc639143029/article/details/89851219

Reference website:

type of data

  • string
  • boolean
  • number
  • object
  • function
  • Symbol (symbol (for ES6 new))

Object Types

  • Object
  • Date
  • Array
  • RegExp Regular
  • Function

It does not contain any type of data value

  • null
  • undefined

JavaScript data types map

JavaScript data types map

typeof operator

You can view the data by type of JavaScript variable typeof.

constructor property

constructor constructor property returns all JavaScript variables.

JavaScript type conversions

js variable can be converted to the new variable or other data types

Conversion method
  • By using a conversion function js
  • Js itself can be automatically switched by
Number turn String
  • String()
  • toString()
  • toExponential () converts the value of the object to exponential notation.
  • toFixed () is converted to the digital string has a specified number of decimal places of the result.
  • toPrecision () digital format for the specified length.
boolean 转 String
  • String()
  • toString()
Number turn string
  • Number()
  • parseFloat () parses a string and returns a floating point number.
  • the parseInt () parses a string and returns an integer.

Automatic conversion type

  • When JavaScript attempt to operate a "wrong" data type is automatically converted to "correct" data type.
5 + null    // 返回 5         null 转换为 0
"5" + null  // 返回"5null"   null 转换为 "null"
"5" + 1     // 返回 "51"      1 转换为 "1"  
"5" - 1     // 返回 4         "5" 转换为 5
  • Unary +
    the Operator + can be used to convert a digital variable:
var y = "5";      // y 是一个字符串
var x = + y;      // x 是一个数字
  • Converted to a digital date
    unary + Another common use is a date (Date) type cast object is converted to digital, the result is returned Unix timestamp.
var time = new Date()
+time
Explicit parsing a string

Number-resolution string and to convert a string of digital mandatory return results are digital. But there are significant differences between parsing and conversion. such as:

var a = "42"
var b = "42px"

Number(a)   //42
parseInt(a) //42

Number(b)   //NaN
parseInt(b) //42

Analytical allow the string contains non-numeric characters, resolved from left to right, if they are non-numeric character is stopped. The conversion does not allow non-numeric characters, otherwise it will fail to return NaN.

Parse the string can float parseFloat () function. ES5 start from the parseInt () is converted into a decimal number default, unless the second parameter specifies as the base.

Equality comparison between null and undefined

In the null and undefined == equal, which means that in the null and undefined == is one thing to be implicit cast another.

  • Master "abstract equal comparison algorithm," Why do readers to tear down [] ==! [] Returns true.

  • "", "\ N" (or "" other spaces in combination) or the like is converted to an empty string is 0 ToNumber mandatory.

ToPrimitive (into the original value)

ToPrimitive operators accept a value, and an optional parameter for the desired type. ToPrimitive operator to which the parameter values ​​into a non-object types. If the object has the ability to be converted to more than one primitive type, you can use the optional type expected to suggest that type. To complete the conversion according to the following table

Note: null is a unique type value is returned substantially by the object detecting typeof (note that 'substantially' word)

Specific reasons:

不同的对象在底层都表示为二进制
在JavaScript中二进制前三位为0的话都会被判断为object类型
null的二进制表示全是0,自然前三位也是0
所以 typeof null === “object”

Guess you like

Origin blog.csdn.net/zc639143029/article/details/89851219