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/03 Report--
Editor to share with you there are several basic data types of javascript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Javascript has five basic data types, namely: 1, Undefined type; 2, Null type; 3, Boolean type; 4, Number type; 5, String type.
This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer.
What are the basic data types of js?
There are five simple data types (also known as basic data types) in ECMAScript: Undefined, Null, Boolean, Number, and String. There is also the complex data type in 1-Object,Object is essentially made up of an unordered set of name-value pairs.
Undefined, Null, Boolean and Number all belong to the basic type. Object, Array, and Function belong to the reference type, and String is a bit special, which will be analyzed below.
Variable
The var keyword is used in ECMAScript to define variables, because js is weakly typed, so it is impossible to determine what value the variable must store, so you do not know what type the variable will be, and the type of the variable can be changed at any time.
This is why ECMAScript is a loose type, which can be used to hold any type of data.
Ps:
The let command is added to es6 to declare variables, and the const command declares a read-only constant.
The use of let is similar to that of var, but the variables declared are valid only within the block of code where the let command is located.
Once const is declared, the value of the constant cannot be changed.
We will not discuss let and const here.
Typeof operator
Because variables in js are loosely typed, it provides a way to detect the data type of the current variable, namely the typeof keyword.
Through the typeof keyword, the following values (displayed as strings) are returned for the five data types
Undefined-if the value does not have a Undefined defined
Boolean-if this value is a Boolean Boolean
String-if this value is the string String
Number-if this value is of numeric type Number
Object-if this value is an object or null Object
It is important to note that typeof null returns object because the special value null is considered an empty object reference.
Undefined
The Undefined type has only one value, the special undefined. When you declare a variable using var but do not initialize it, the value of this variable is undefined. However, it is generally recommended that variables be initialized as much as possible, but the value of undefined was not specified in earlier versions of js, so in some frameworks undefined values are added to window objects for compatibility with older browsers.
Window ['undefined'] = window [' undefined']; / / or window.undefined = window.undefined;Null
The Null type is the second data type that has only one value, and this special value is null. From a logical point of view, a null value represents an empty object pointer, which is why object is returned when you use the typeof operator to detect null.
Var car = null; console.log (typeof car); / / "object"
If the defined variable is intended to be used to hold the object in the future, it is best to initialize the variable to null rather than any other value. In this way, as long as you directly detect the null value, you can know whether the corresponding variable has saved a reference to an object.
For example:
If (car! = null) {/ / perform some operations on car objects}
In fact, undefined values are derived from null values, so ECMA-262 dictates that tests for their equality return true.
Console.log (undefined = = null); / / true
Although null and undefined have such a relationship, their uses are completely different. In any case, it is not necessary to explicitly set the value of a variable to undefined, but the same rule does not apply to null. In other words, you should explicitly let the variable hold the null value as long as the variable intended to save the object has not actually saved the object. This not only reflects the convention of null as a null object pointer, but also helps to further distinguish between null and undefined.
Boolean
This type has only two literals: true and false. These two values are not the same as numeric values, so true is not necessarily equal to 1, and false is not necessarily equal to 0.
Although there are only two literal values of type Boolean, all types of values in JavaScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can call the type conversion function Boolean (), for example:
Var message = 'Hello World'; var messageAsBoolean = Boolean (message)
In this example, the string message is converted to a Boolean value, which is stored in the messageAsBoolean variable. The Boolean () function can be called on a value of any data type, and a Boolean value is always returned. Whether the value returned is true or false depends on the data type of the value to be converted and its actual value. The following table shows the conversion rules for various data types and their objects.
Data type converted to true value converted to false value BooleantruefalseString any non-empty string "" (empty string) Number any non-zero numeric value (including infinity) 0 and NANObject any object nullUndefined does not apply undefined var message = 'Hello World'; if (message) {alert ("Value is true");}
Running this example displays an alert box because the string message is automatically converted to the corresponding Boolean value (true). Because of this automated Boolean transformation, it is important to know exactly what variables are used in the flow control statement.
Ps: use! Operator to convert Boolean values
!! Generally used to cast the following expression to Boolean data (boolean), that is, it can only be true or false
For other values that use implicit conversion, such as null and undefined, use! Operator produces the result of true, so the purpose of using two exclamation points is to convert these values to "equivalent" Boolean values.
In the case of var foo; alert (! foo); / / undifined, an exclamation point returns true; alert (! goo); / / in the case of null, an exclamation point also returns true; var o = {flag:true}; var testfully contains equivalent to var test=o.flag | | false; alert (test)
This example demonstrates that in undifined and null, both true is returned with an exclamation point, and false is returned with two exclamation points, so the purpose of the two exclamation points is that if you explicitly set the value of the variable (non-null/undifined/0/ "" equivalent), the result will be returned according to the actual value of the variable, and if it is not set, the result will return false.
[recommended study: javascript basic tutorial]
Number
This type is used to represent integer and floating-point values, as well as a special value, NaN (non-numeric Not a Number). This value is used to indicate that an Operand that is supposed to return a value does not return a value (so that an error is not thrown). For example, in other programming languages, any numeric value divided by 0 causes an error, which stops code execution. In JavaScript, however, any numeric value divided by 0 returns NaN, so it does not affect the execution of other code.
NaN itself has two unusual features. First, any operation involving NaN (such as NaN/10) returns NaN, which can cause problems in multi-step calculations. Second, NaN is not equal to any value, including NaN itself. For example, the following code returns false.
Alert (NaN = = NaN); / / falseString
The String type is used to represent a sequence of zero or more 16-bit Unicode characters, or strings. Strings can be represented by single quotation marks (') or double quotation marks (").
The particularity of String type
The string type is somewhat special because a string has a variable size, so it is clear that it cannot be stored directly in a variable with a fixed size. For efficiency reasons, we want JS to copy only the references to the string, not the contents of the string. On the other hand, a string behaves like a basic type in many ways, and the fact that a string is immutable (that is, the content of a string value cannot be changed), so you can think of a string as an immutable reference type that behaves like a basic type.
Boolean, Number, and String are the basic wrapper types in Javascript, that is, these three are actually a constructor. They are instances of Function and reference types. As for the String here and the String mentioned above, they have the same name, because in fact, String refers to the string, and the String here refers to the constructor String, which is written above for better understanding, because Javascript is a loose type. We can look at an example of String instantiation:
Var name = String ("jwy"); alert (typeof name); / / "string" var x=new String ('12345') typeof x / / objectx='12345'typeof x / / stringvar author = "Tom"; alert (typeof name); / / "string"
As for author, there will be length,substring and other methods, in fact, string is just an example of String, similar to String in C #, and string.
Note that the typeof variable is a string if the value is "string". In Javascript, the string is the basic type, while in C # or Java, the string is the reference type, but the String in Javascript is the reference type because it is the basic wrapper type defined in Javascript. In C #, String is the same as string.
This post is only a brief copy of some JavaScript advanced programming (third edition) content, plus their own focus on the point of view, friends to read this post or to read ah, here is just a reference.
These are all the contents of the article "javascript has several basic data types". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.