This is the 13th day of my participation in the August More Text Challenge
Knowing that you are familiar with JavaScript types, how many of the following types can you answer?
- String Does a String have a maximum length?
- Is 0.1+0.2 == 0.3 correct?
- Is 3 the same as Number(3)? Is 3 the same as the new Number(3)?
- What does typeof NULL get?
.
Read the article to find the answer!
preface
The JavaScript language specifies the following seven language types, and every value in the JavaScript language belongs to a certain data type.
Basic types of
-
Undefined;
-
Null;
-
Boolean;
-
The String;
-
Number;
-
Symbol; (New in ES6)
Reference types
- The Object.
Undefined type
Undefined means Undefined
- It has only one value, undefined
- Any variable is of Undefined type and value Undefined before assignment
- Undefined is a global variable created at runtime, not a JavaScript keyword
Undefined is read-only after ES5. Undefined = 123 or var undefined = 12 at the beginning will not work (except in older browsers), but in node or local scopes, It is allowed to redeclare a global variable of undefined with var undefined = 123 or let undefined = 123
Null type
Null means “defined but Null” and its semantics represent Null values.
-
The Null type also has only one value, which is Null
-
Null is the JavaScript keyword
-
A variable can be emptied by assigning its value to NULL
Boolean type
Boolean types are used to represent true and false in a logical sense, with the keywords true and false to represent two values.
Type String
String is used to represent text data.
Q: Does a String have a maximum length?
“A”
String has the maximum length, and the length is not the visual returned character length, but the length of the UTF16 code point. The maximum length of String is 2^ 53-1.
“Note 1”
The meaning of String is not “String”, but the UTF16 encoding of String. Our String operations charAt, charCodeAt, length and other methods are targeted at UTF16 encoding.
Let a = '𠮷'; a.lengthCopy the code
“2”
Strings in JavaScript can never be changed. Once a string is constructed, you can’t change the contents of the string in any way, so strings have the characteristics of value types.
For example: let a = ‘Axjy’, you can’t change it to ‘Bxjy’ without reassigning;
The Number type
The Number type means “Number” in our usual sense. There is only one Number type in JavaScript. The Number type uses the “double precision floating point Number” in IEEE754 standard to represent a Number, and does not distinguish between integer and floating point Number.
Q: is 0.1+0.2 == 0.3 correct?
“A”
We can look at 0.1+0.2 == 0.3, the result is false, indicating that they are not equal, which is a feature of floating point arithmetic;
The result is that the left and right sides of the equations are not exactly equal, but slightly different.
What is the reason for the accuracy problem of floating-point arithmetic?
“A”
Because we input a decimal number, but the computer converts it to binary, and then converts it back to decimal, and there’s a loss of accuracy in the process.
This is a problem with any programming language that uses binary floating-point numbers, but because JavaScript is a weakly typed language, the problem of schedule errors is particularly acute.
The calculation process of “0.1 + 0.2” in the computer is as follows:
Specific numerical
-
Infinity stands for Infinity in mathematics.
-
NaN (non-number) is the result of incorrect or undefined mathematical operations.
Symbol type
Symbol, a new type introduced in ES6, is a collection of all non-string object keys.
Q: What is the output of the following code?
let A = Symbol("Axjy"); let B = Symbol("Axjy"); console.log(A == B) Or console.log(A === B)Copy the code
“A”
Either is false. Symbols can have string descriptions, but symbols are not equal even if the descriptions are the same.
The Object type
Object is the most complex type in JavaScript and one of the core mechanics of JavaScript. Object is an Object. It is a general name for all tangible and intangible objects.
Q: Is 3 the same as Number(3)? Is 3 the same as the new Number(3)?
👇
👇
👇
👇
👇
👇
👇
👇
“A”
3 is the same as Number(3); 3 is a completely different value from new Number(3); one is of type Number and the other is of type object.
Number, String, and Boolean are dual-use constructors. When paired with new, they produce objects, and when called directly, they represent casts.
other
About judgment types
The JavaScript language provides operations such as typeof to return the typeof the operand, but the result of a typeof operation is sometimes inconsistent with the run-time type specification
“Note”
Object - Null and the function object
conclusion
If any of the above is wrong, please point out in the comments section! 🌷
Reference:
Detail the seven JavaScript data types
ECMAScript Language Specification
Xiao Ke love to finish click a “like” before leaving! 😗