In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will take you to understand what the eight data types of JavaScript are. The knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem find the answer to the problem. Let's follow the editor to learn more about "what are the eight data types of JavaScript".
I. Preface
In ES5, we do recognize six data types: Number, String, Boolean, undefined, object, Null.
A new Symbol has been added to ES6. Objects of this type are never equal, that is, the same value is passed in at the beginning of creation, which can solve the problem of property name conflict and serve as a tag.
There is also a bigInt in Google version 67. Refers to the safe storage and operation of large integers.
So: to be exact, there are a total of 8 JS data types!
Number
String
Boolean
Null
Undefined
Object
Symbol
BigInt
There are basic data types and reference data types by type:
Basic data types: String, Number, Boolean, Null, Undefined, Symbol
Reference data type: Object [Object is a large class, function function, array array, date date. All belong to Object]
Second, dynamic data types
JavaScript has dynamic types. This means that the same variable can be used as different types.
Variables in JavaScript seem simple because they only need a "var" to declare variables, unlike other programming languages that strictly distinguish data types (int/double/char/boolean...). This is also beneficial because a variable can be assigned a value of any type, and it can also be assigned a different type of value to the variable. It's not "a lifetime".
Var x = 1; / / number type var x = 0.1; / / number type, JS does not distinguish between integer and floating point values / / now x is changed from number to stringvar x = "hello world"; / / the string var x = 'javascript'; / / text within single quotation marks can also be formed / / now x is changed from string to booleanvar x = true; / / true and false as Boolean var x = null Var x = undefined; / / null is very similar to undefined and is a special type 2.1string string
A string is a variable that stores characters such as "Bill Gates"
The string can be any text in quotation marks. You can use single or double quotation marks
Use single or double quotation marks, there is no difference
Single quotation marks do not nest single quotes. Double quotation marks are the same. If you want to be nested, you need to escape the quotation marks.
Var str = 'Sun WuKong'; / / single quotation mark string var str2 = 'Zhu Bajie'; / / a double quote string
Other escape characters:
\ n represents a newline character
\ r indicates carriage return
\ t horizontal tab tab
\ 'single quotation marks
\ "double quotation marks
\\ backslash
\ uXXXX four-bit hexadecimal representation of unicode string
\ xXX two hexadecimal bits represent Latin characters
String concatenation:
Var str1 = "hello"; var str2 = "world"; var str = str1 + str2console.log (str); / / helloworld2.2 numeric number
JavaScript has only one numeric type. Numbers can be decimal or not.
Var num1=5.02; / / use decimal point to write var num2=520; / / No decimal point can be used to write var yearly 123e5; / / 12300000var zonal 123e5; / / 0.00123
Range of values:
5e324 ~ 1.7976931348623157e+308
If it is out of range, it will be expressed as Infinity or-Infinity
Special numerical value NaN:
NaN is of type number and is a numeric value
Characteristic 1:NaN performs any operation with any number and the result is NaN
Characteristic 2:NaN is not equal to any number, including oneself
Related functions:
IsNaN () determines whether the data is NaN and returns true, otherwise it returns false
IsFinite () determines whether the data is within the range and returns true within the range, otherwise false
/ / NaN is a special number that is not equal to all other values, including its own console.log (NaN = NaN); / / false// 's unique method to identify NaN, console.log (isNaN (NaN)); / / true// range: 5e324 ~ 1.7976931348623157e+308var min = 5e324 / NaN max = 1.7976931348623157ewings 308ash var a = 1.7976931348623157ewings 309; console.log (min); / / Infinityconsole.log (max); / 1.7976931348623157e+308console.log (a) / / Infinityconsole.log (isFinite (a)); / / false (out of range, out of range) 2.3Boolean boolean
Boolean (logical) can only have two values: true or false
Var xonal truth, var yearly falsehood, 2.4 null and undefined
Null means there is no object, no value here
Undefined indicates a missing value, which should have a value but is not defined
Var x = null;var y = undefined2.5 symbol
The Symbol function can take a string as an argument to provide a description for the newly created Symbol, which can be displayed on the console or used as a string, making it easy to distinguish
Let name = Symbol (); let name2 = Symbol ("bb"); console.log (name); / / Symbol () console.log (typeof name); / / "symbol" console.log (name,name2); / / Symbol () Symbol (bb) console.log (name = name2); / / false2.6 object object
Objects are separated by curly braces. Inside the parentheses, the properties of the object are defined in the form of key-value pairs (name: value). Properties are separated by commas
/ / spaces and line breaks do not matter. The declaration can span multiple lines var obj = {"name": "Zhang San", "age": 18}
(here the key name can also be without quotation marks.)
Read the properties in the object:
Name = obj.name// or name = obj ['name'] III. Differences between basic data types and reference data types 3.1 different memory allocations when variables are declared
Because the space occupied by the basic data type is fixed and small, the basic data type is stored in the stack, that is, where the variable is accessed.
The reference data type is stored in the heap, and the variable accesses a pointer to the memory address of the storage object.
3.2 precisely because of different memory allocations, the results are not the same when copying variables
After the basic data type is copied, the two variables are independent because the value is copied.
The reference data type copies a pointer, and the value of two variables is what the pointer points to. Once one party modifies it, the other party will also be affected.
Var x = 3; var y = x; console.log (XMagol y); / / 3 3 / / after the basic data type is copied, the two variables are independent x = 5; / / modify one side, and the other is not affected by console.log (XMague y); / / 5 3 var a = [0Magol 1 ~ 2 Magne3]; var b = a; console.log (a, b) / / (4) [0,1,2,3] (4) [0,1,2,3] a [0] = 9 console.log (a, b); / / (4) [9,1,2,3] (4) [9,1,2,3]
Results:
Here you can clearly see that b copied a, changed the value of array a, and so did array b. This is because when the reference data type copies the same pointer instead of the same value, the pointer points to the same address, and one party's modification of the other will be affected!
(the deep and shallow copies of JS are involved here, so I won't go into detail here. I will explain it in a separate article later.)
3.3 parameters are passed differently
The basic data type passes the value in the variable to the parameter, after which the parameter and the variable do not affect each other.
Reference data type (here take the function as an example): although the parameters of the function are passed by value, the value passed by the reference value is a memory address, and the actual parameter and the formal parameter point to the same object, so the modification to this parameter within the function will be reflected externally.
The conversion of data types and the methods of detecting data types will be explained separately in other articles, so I won't go into detail here for convenience.
Thank you for your reading, the above is the whole content of "what are the eight data types of JavaScript?" learn the friends to hurry up to operate it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.