Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the types of values for javascript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the types of javascript values". In daily operation, I believe many people have doubts about what types of javascript values there are. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what types of javascript values are there?" Next, please follow the editor to study!

There are six value types of javascript, namely: 1, String type; 2, Number type; 3, Boolean type; 4, Null type; 5, Undefined type; 6, Symbol type, which represents a unique value.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, value types are also called basic data types. They are:

String (String), number (Number), Boolean (Boolean), empty (Null), undefined (Undefined), Symbol

Tip: Symbol is a new data type introduced in ECMAScript6 that represents a unique value.

1. String type

A string (String) type is a piece of text wrapped in single or double quotes, such as' 123', "abc". It is important to note that single and double quotes are different ways to define a string and are not part of a string.

When defining a string, if the string contains quotation marks, you can use the backslash\ to escape the quotation marks in the string, or select a different quote from the string to define the string, as shown in the following example:

Var str = "Let's have a cup of coffee."; / A double quotation mark contains single quotation marks var str ='He said "Hello" and left.'; / / single quotation marks contain double quotation marks var str ='We\'ll never give up.'; / / escape single quotation marks in a string using a backslash

2. Number type

Numeric (Number) types are used to define numeric values. Integer and decimal numbers (floating point numbers) are not distinguished in JavaScript, and are uniformly represented by Number type, as shown in the following example:

Var num1 = 123; / / integer var num2 = 3.14; / / floating point number

Note: the values that can be defined by the Number type are not infinite, and the Number type in JavaScript can only represent values between-(253-1) and (253-1).

Some very large or very small numbers can also be expressed by scientific (exponential) counting, as shown in the following example:

Var yearly 123e5; / / 123x 10 to the fifth power, that is, 12300000var zonal 123emur5; / / 123x 10 to the-5th power, that is, 0.00123

In addition, there are some special values in the Number type, which are Infinity,-Infinity, and NaN, where

Infinity: a number used to represent positive infinity, usually a number greater than 1.7976931348623157e+308

-Infinity: used to represent a negative infinity, usually a number less than 5e-324

NaN: non-numeric (abbreviation for Not a Number), used to represent invalid or undefined mathematical operations, such as 0 divided by 0.

Tip: if the result of a calculation is out of the range of Number type in JavaScript, the number will be automatically converted to infinity, with positive number Infinity and negative number-Infinity.

3. Boolean type

The Boolean type has only two values, true (true) or false (false), which are often used in condition judgment. In addition to directly using true or false to define Boolean variables, you can also get Boolean values through some expressions, such as:

Var a = true; / / define a Boolean value truevar b = false; / / define a Boolean value falsevar c = 2 > 1; / / expression 2 > 1 holds, the result is "true (true)", so the value of c is truevar d = 2 < 1 of Boolean type; / / expression 2 < 1 is not true, the result is "false", so the value of c is false of Boolean type

4. Null type

Null is a special data type with only one value, indicating a "null" value, that is, there is no value, nothing, and is used to define a null object pointer.

Using the typeof operator to look at the type of Null, you will find that the type of Null is Object, indicating that Null actually uses a special value that belongs to Object (object). So by assigning the variable to Null, we can create an empty object.

5. Undefined type

Undefined is also a special data type with only one value, indicating that it is undefined. When we declare a variable without assigning a value to it, the default value for that variable is Undefined. For example:

Var num;console.log (num); / / output undefined

When you use the typeof operator to look at unassigned variable types, you will find that they are also of type undefined. If you look at the type of an undeclared variable using the typeof operator, you will find that the undeclared variable is also undefined. The sample code is as follows:

Var message;console.log (typeof message); / / output undefinedconsole.log (typeof name); / / output undefined

6. Symbol type

Symbol is a new data type introduced in ECMAScript6, which represents a unique value. Values of type Symbol need to be generated using the Symbol () function, as shown in the following example:

Var str = "123"; var sym1 = Symbol (str); var sym2 = Symbol (str); console.log (sym1); / / output Symbol (123) console.log (sym2); / / output Symbol (123) console.log (sym1 = = sym2) / / output false: although sym1 and sym2 look the same, they are actually not the same. According to the characteristics of the Symbol type, sym1 and sym2 are unique here, and the study of "what types of javascript values are there" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report