JavaScript data types are divided into two types: basic data types: String, Number, Boolean, undefined, and NULL, and complex data types: Object. JavaScript does not support any mechanism for creating custom types, and all values will eventually be one of the six data types mentioned above.

First, let’s look at the basic data types.

1. String

JavaScript uses the Unicode character set encoded in UTF-16. Strings in JavaScript are a sequence of unsigned 16-bit values. Most commonly used Unicode characters are represented by 16-bit internal codes and represent individual characters in the string

var p = 'PI.
var e = 'e'
p.length / / 1. P contains a 16-bit value
e.length // e encoded by UTF-16 contains two 16-bit values: "\ud835\ UDc52"
Copy the code

Whenever a String attribute is referenced, JavaScript converts the String value to an object via a new String(s), which inherits the String’s methods. Once the reference ends, the newly created object is destroyed

var s = 'test'
s.len = 4
var t = s.len // undifined
Copy the code

The second line creates a temporary object and assigns a value to its Len attribute, which it then destroys. The third line uses the original string value, and therefore I pay because I lack len

When a string property value is read, it behaves like an object. But if you try to assign a value to an attribute, this operation is ignored, and the modification only happens to the temporary object, which is not saved

This temporary object is called a wrapper object, and string (as well as numeric and Boolean values) attributes are read-only and cannot be assigned, unlike other objects

Strings are stored in heap memory and cannot be changed once created. If you want to change the string held by a variable, you must destroy the original string and fill the variable with a new one.

2. Number

In JavaScript, all numbers are of type Number, which is divided into integer values and floating point values, but the memory for floating point numbers is twice as large as the memory for integers, So JavaScript certainly doesn’t want to store too many floating points in too much memory. There are two ways to convert floating points to integers:

  1. The decimal point is not followed by a number, like 1.
  2. It’s 0 after the decimal point, like 1.00

They’re all going to be integers 1

There is also a NaN(Not a Number) in the Number type. What if an interviewer asks you how to tell if a variable is a NaN? You can’t answer x == NaN, of course, because this particular value is not equal to any value, and it takes real work to determine it. There are two ways to determine:

  1. usex ! = xIf x returns true, then x is NaN
  2. JavaScript provides us with functionsisNaN()For our convenience.

Any type of data is converted to a Number when performed mathematically with data of type Number, and NaN is the one that cannot

3. Boolean

There’s nothing to say about Boolean types, just two values: true and false, but it’s important to note that in JavaScript, casting offers a lot more possibilities, so let’s take a quick look at other data types being converted to Booleans.

Numeric types Converts to a Boolean value
undefined false
null false
Boolean value true/false
digital +0, -0, and NaN are false, and the others are true
string Empty strings are false, others are true
object true

Any value in JavaScript can be converted to a Boolean value, of which only six are converted to false

4. Undefined

The Undefined type has only one value, Undefined. This value is used to represent variables that have been declared but not assigned.

5. Null

Null also has a single value: Null, which represents an empty object pointer.

Undefined = null; undefined = null; undefined = null; Null means there are no objects, and there should be no values

The typeof operator

The Typeof operator is a useful tool for determining basic data types.

Typeof checks the data typeof a variable and returns six values

  • 'number'
  • 'string'
  • 'boolean'
  • 'object'
  • 'undefined'
  • 'function'

Notice that these are all string types, so if we write typeof(typeof(‘123’)), we return ‘string’.

In JavaScript, values of primitive types are stored in stack memory, assignments between variables are copied, and the two variables do not interfere with each other in any subsequent operation

Say that finish after the basic data type is a complex type, complex type itself contains a lot of, later I will each kind of complex types for details, welcome to pay attention to the interviewer to impress series, if there is wrong or better opinions, please point out, I wish you all can get the favour of the interviewer, find a job as soon as possible!