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 five basic data types of JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The editor will share with you the five basic data types of JavaScript. I hope you will get something after reading this article. Let's discuss it together.

JavaScript is a weakly typed scripting language, and you do not need to specify the data type of a variable when you declare a variable. The data type of the JavaScript variable is dynamically determined at the time of interpretation. But the value of JavaScript is kept in memory and is also of data type. There are five basic data types of JavaScript

(1) numerical type

(2) Boolean type

(3) string type

(4) Undefined type

(5) Null type

1. Numerical type

Unlike strongly typed languages such as C and Java, the numerical type of JavaScript includes not only all integer variables, but also all floating-point variables. The values in JavaScript language are saved in IEEE 754 double-precision floating-point format. The numerical situation in JavaScript is so rich that it is fully supported by the scientific counting method. The scientific counting method, such as 512, represents 5.12 times 10 to the second power, and 512 also represents 5.12 times 10 to the second power.

In scientific counting, E is an interval symbol, and E is not case-sensitive.

/ / explicitly declare variables a, b var a, b; / assign values to a, b using scientific notation, which should be 500a = 5E2; b = 1.23emur3; / / use the warning prompt box to output the value of variable an alert (a + "\ n" + b)

Note: the direct quantity of the value should not start with 0. Because JavaScript supports octal and hexadecimal. Octal starts with 0 and hexadecimal starts with 0x or 0X.

When a numeric type is outside the scope of its representation, two special values appear: Infinity (positive infinity) and-Infinity (negative infinity). )

/ define x as the maximum value var x = 1.7976931348623157e308; / / increase the value of x again x = x + 1e292; / / use the warning box to output the value of x alert (x)

two。 String type

The substring of JavaScript must be enclosed in quotation marks, which can be either single or double quotation marks.

Var a = "12345678912aaa"; var a = '12345678912aaaa'

Note: there are two main differences between strings in JavaScript and Java:

(1) strings in JavaScript can be enclosed in single quotation marks

(2) use = = to compare whether the character sequences of two strings are equal in JavaScript, instead of using the equals () method.

JavaScript represents a string as a String built-in class. The String class contains a series of method operation strings. The string class has the following basic method and attribute operation strings:

(1) charCodeAt (): returns the Unicode value corresponding to a character at a specific index in a string

(2) Legth (): returns the length of the string

(3) toUpperCase (): convert all lowercase letters in stringObj to uppercase letters

(4) toLowerCase (): convert all uppercase letters in stringObj to lowercase letters

(5) fromCharCode (): converts a series of Unicode values into strings by calling methods directly through the String class.

(6) indexOf (): returns the position where the string first appears

(7) lastIndexOf (): returns the position where the string last appeared

(8) subString (): intercept all characters from start to end in stringObj, including characters at start, but excluding characters at end

(9) slice (): intercepts all characters from start to end in stringObj, including characters at start, but excluding characters at end. Both start and end can be negative values, which, when negative, represent the nth character from the last character, such as-1 for the last character and-2 for the penultimate character.

(10) match (): retrieves the matching result of a specified regular expression within a string, which is related to whether regexp has a specified global flag g

(11) split (): separates the separtor and splits the stringObj into an array of strings. Separator can be a string or a regular expression, with separator as the delimiter if it is a string, and a string that conforms to the pattern specified by separator if the weak separator is a regular expression.

(12) replace (): replaces a substring in a string with a specific string.

/ / define the string variable a var a = "abc China"; / / get the length of a, var b = a.Unicode; / / convert the Unicode value of the series into the string var c = String.fromCharCode (97 and 98) / / the length of the output a, the character of the string an at index 4 and the corresponding Unicode value of / /, and the value of the c string variable alert (b + "- -" + a.charAt (4) + "- -" + a.charCodeAt (4) + "- -" + c)

Here are some common escape characters

Order

Escape character

instructions

0

NUL character (\ u0000)

one

\ b

Backspace (Backspace) backspace character (\ u0008)

two

\ f

Page change (Form Feed) (\ u000C)

three

\ n

Line feed (New Line) (\ u000A)

four

\ r

Enter (Carriage Return) (\ u000D)

five

\ t

Tabulation (Tab) horizontal tabs (\ u0009)

six

\'

Single quotation marks (\ u0027)

seven

\ "

Double quotes (\ u0022)

eight

\\

Backslash (Backslash) (\ u005C)

nine

\ v

Vertical Tab (\ u000B)

ten

\ xNN

The Latin-1 character specified by the two-digit hexadecimal numeric NN

eleven

\ uNNNNN

The Unicode character specified by the four-digit hexadecimal number NNNN

twelve

\ NNN

The Latin-1 character specified by one to three octets (1 to 377).

ECMAScript v3 does not support it. Do not use this escape sequence.

3. Boolean type

There are only two values of Boolean type: true and false. Boolean values are usually the result of logical operations or are used to mark some state of an object.

/ / if the browser supports Cookie if (navigator.cookieEnabled) {alert ("browser allows Cookie");} / / if the browser does not support Cookie else {alert ("browser disables Cookie");}

4. Undefined and null

The value of type Undefined has only one undefined, which is used to indicate that a variable does not exist or that no value is assigned to it, as well as that the property of the object does not exist. Null is used to indicate that the value of the variable is empty. The difference between Undefined and null is subtle. In general, undefined means that no value is set for the variable or the property does not exist, while null means that the variable has a value, only that its value is null.

But many times undefined and null themselves want to wait without an exact comparison, that is, null==undefined will return true. If you want to make a precise distinction between null and undefined, you should consider using the exact equals (=)

/ / declare variables x, y var x, y = null; / / determine whether the value of x is empty if (x = undefined) {alert ('default value after variable declaration is undefined');} if (x = null) {alert (' default value is null' after variable declaration) } / / determine whether x (whose value is undefined) is equal to y (its value is null) if (x = = y) {alert ("x (undefined) = = y (null)") } / / Test a non-existent attribute if (String.xyz = undefined) {alert ("non-existent property defaults to undefined");} 5. Regular expression

The essence of a regular expression is a special string, which allows the use of "wildcards", so a regular expression string can match a group of ordinary strings.

Metacharacter name matching object

. A single arbitrary character of the period (except carriage return\ r, newline\ n, line separator\ u2028 and segment separator\ u2029)

A single arbitrary character listed in the [] character group

[^] exclude a single arbitrary character not listed in the type character group

? Question marks match 0 or 1 times

* asterisks match 0 or more times

+ plus sign matches one or more times

{min,max} interval quantifiers match at least min times, up to max times

The starting position of the ^ delimited line

The end of the $dollar line

| | any expression separated by a vertical bar |

() parentheses limit the scope of the multi-selection structure, mark the elements of the quantifier function, and capture text for reverse reference.

1, 2. The first and second before the reverse reference match. Text matched by expressions in group parentheses

\ 0 NUL character\ u0000 [\ b] matches the backspace character\ u0008 Don't be confused with\ b\ t tab\ u0009\ nLine feed\ u000A\ v vertical tab\ u000B\ f feed\ u000C\ r carriage return\ u000D\ xnn Latin character specified by hexadecimal number nn\ uxxxx Unicode character specified by hexadecimal number xxxx (\ u4e00 -\ u9fa5 for Chinese)\ cX control character ^ X, indicating ctrl- [X] Where X is any letter of Amurz, which is used to match control characters {n} match n times {n ·m} at least n times, m times at most {n,} at least n times? Equivalent to {0jue 1} * equivalent to {0,} + equivalent to {1,} after reading this article, I believe you have a certain understanding of "which five basic data types of JavaScript". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

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