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--
Editor to share with you what are the basic data types in JavaScript. I hope you will get something after reading this article. Let's discuss it together.
Case-sensitive
In JavaScript, everything (variables, functions, and operators) is case sensitive. This means that the variable name test and the variable name Test represent two different variables.
II. Identifier
The so-called presentation identifier refers to variables, function names and operators, or parameters of a function. The format rules for markers are as follows:
1. The first character must be a character, an underscore (_) or a dollar sign ($)
2. Other characters can be letters, underscores, dollar signs and numbers
By convention, JavaScript identifiers are written in a small hump format, that is, opening a lowercase letter and capitalizing the first letter of the remaining words. For example: firstChirld, myCar
Of course, it is not mandatory for identifiers to be written in a small hump format, but it is easy for us to understand the code and try to write in a small hump format to facilitate others to understand your code.
III. Notes
Comment methods in JavaScript are divided into single-line comments and multi-line comments:
Single-line comments:
/ / this is a single-line comment
Multiline comments:
/ * this is a * multi-line comment * / IV.
In JavaScript, statements end with a semicolon; if the semicolon is omitted, it is up to the interpreter to determine the end. As shown in the following example:
Var sum = a + b / / is a valid statement even without a semicolon-- var diff = a-b is not recommended; / / valid statement-- recommendation 5. Variables
Variable types in JavaScript are weakly typed, which means that they can be used to hold any type of data. In other words, each variable is just a placeholder to hold the value.
Variables are defined with the var keyword, followed by the variable name (that is, the identifier). As follows:
Var message
JavaScript also supports direct initialization of variables, so you can set the value of a variable while defining it. As follows:
Var message = "Hello world!"
It is important to note that a variable defined with the var keyword will become a local variable in the scope where the variable is defined. That is, if var is used in a function to define a variable, the variable will be destroyed after the function has finished running. For example:
Function test () {var message = "Hello"; / / Local variable} test (); console.log (message); / / error!
The reason for the error is that the variable message is defined using var in the function test (). When a function is called, a variable is created and assigned to it. After that, the variable is destroyed immediately, so the next line of code in the example will cause an error. However, you can create a global variable by omitting the keyword var as follows:
Function test () {message = "Hello"; / / Global variable} test (); console.log (message); / / "Hello"
If you omit the keyword var,message here, it becomes a global variable. In this way, as long as the test () function is called once, the variable is defined, and once the global variable is set, it can be accessed anywhere outside the function.
Although global variables can be defined by omitting the keyword var, it is not recommended because global variables defined in local scope are difficult to maintain
Data type
There are several simple data types (also known as basic data types) in JavaScript: Undefined, Null, Boolean, Number, and String. There is also a complex type-- Object,Object is essentially an unordered set of name-value pairs. No mechanism for creating custom types is supported in JavaScript, and all values are one of these six data types.
Typeof operator
Because JavaScript is weakly typed, you need a way to detect the data type of a given variable-- typeof is an operator responsible for detecting the data type of a given variable. Using the typeof operator on a value may return one of the following strings:
"undefined"-if this value is not defined
"boolean"-if this value is of Boolean type
"string"-if this value is a string
"number"-- if this value is a number
"object"-if this value is an object or null
"function"-if this value is a function
Here are a few examples of using the typeof operator:
Var message = "hello"; console.log (typeof message); / / "string" console.log (typeof (message)); / / "string" console.log (typeof 95); / / "number"
The operands of the typeof operator can be variables or numeric literals. Note that typeof is not a function, so although the parentheses in this example can be used, they are not required.
In JavaScript, a function is an object, not a data type, so it is necessary to use typeof to distinguish a function from other objects.
Undefined Typ
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. For example:
Var message;console.log (message = = undefined); / / truevar message1 = "undefined"; console.log (message1 = = undefined); / / true
What is confusing, however, is that executing the typeof operator on an uninitialized variable returns an undefined value, while executing the typeof operator on an undeclared ratio variable also returns an undefined value. Take a look at the following example:
Var message; / / the variable is declared with an undefined value by default / / the following variable does not declare / / var age;console.log (typeof message); / / "undefined" console.log (typeof age); / / "undefined"
Undefined is returned for both initialized and declared variables, which is logically reasonable. Because the two variables are essentially different from a technical point of view, but in fact, it is impossible to perform a real operation on that kind of variable.
Null Typ
The Null type is the second one with only one worthy type, and this special value is null. From a logical point of view, a null value represents a null pointer to an object, which is why "object" is returned when detecting a null value using the typeof operator, as shown in the following example:
Var message= null; console.log (message); / / "object"
If the defined object is used to hold the object in the future, it is best to initialize this variable to null rather than any other value. In this way, you can directly know whether the corresponding variable has saved a reference to an object by directly checking the null value, as in the following example:
If (message! = null) {/ / A pair of message performs a series of operations}
In fact, the value of undefined is derived from null merit, so when you test them for equality, return true:
Console.log (null = = undefined); / / true
In this case, the equality operator (= =) between null and undefined always returns true. Although null and undefined have such a relationship, their uses are completely different. As mentioned above, in any case, it is not necessary to represent a variable as undefined, but the same rule does not apply to null. In other words, only the variable that holds the object does not actually save the object, it should explicitly indicate that the variable should be saved as a null value. This not only reflects the convention of null values as null pointers, but also helps to further distinguish between null and undefined.
Boolean Typ
The Boolean type is the most frequently used type in JavaScript, with only two literal values: 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. Here is an example of assigning a value to a variable:
Var first = true;var second = false
It is important to note that the two values of the boolean type are case sensitive. In other words, True is not the same thing as true, False and false.
True and False (and other mixed-size forms) are just identifiers, not Boolean values.
Although Boolean values have only two values, values of all types of JavaScript have values equivalent to these two Boolean values. To convert a value to the corresponding Boolean value, you can call the Boolean () function. For example:
Var message = "Hello world!"; var messageAsBoolean = Boolean (message); console.log (messageAsBoolean); / / true
The Boolean () function can be called on any type of value, and a Boolean value is always returned. Whether true or false is returned depends on the actual type to be converted and its actual value. The following table shows the corresponding rules for the conversion of various data types:
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 nullUndefinedn/aundefinedNumber type
The number type defines different numeric literal formats in JavaScript. For example:
Var intNumber = 55; / / decimal integer var octalNum1 = 070; / / octal 56 var octaNum2 = 079; / / invalid octal number, parsed as 79 var octaNum3 = 08; / / invalid octal number, resolved as 8 var hexNum1 = 0xA / / hexadecimal 10 var hexNum2 = 0x1f; / / hexadecimal 31
In arithmetic calculations, all octal and hexadecimal values are converted to decimal values.
NaN
NaN, which is not a numeric value, is a special numerical value. This number is used to indicate a situation where an Operand that was supposed to return a value does not return a value (so that an error is not thrown). NaN itself has two characteristics:
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:
Console.log (NaN = = NaN); / / falseString type
The string type is used to represent a sequence of zero or more Unicode characters, that is, strings. Strings can be represented by double quotation marks (") or single quotation marks ('), so the following two ways of writing are valid:
Var firstName = "Wang Wenzheng"; var secondName = "Wang Bin"
Those that begin with double quotes must end with double quotes, while those that begin with quotes must end with single quotes. The following can lead to syntax errors:
Var name = "Wang Bin"; / / literal amount of grammatical error characters
The String type contains some special literals, also known as escape sequences, that are used to represent non-print characters or characters for other purposes. The literals of these characters are shown in the following table:
Literal meaning\ nnewline\ t tabulation\ bBackspace\ r return\ f feed\ slash 'single quotation mark "double quotation marks\ xnn represents a character in hexadecimal code nn\ unnn the characteristic of a Unicode character string in hexadecimal code nnn
Strings in JavaScript are immutable, that is, once a string is created, its value is immutable. To change the string saved in a variable, first destroy the original string, and then populate the variable with another string containing the new value, for example:
Var lang = "java"; lang = lang + "script"; console.log (lang); / / javascriptObject type
An object in JavaScript is a collection of data and functions. Objects can be created by executing the new operator followed by the name of the object type to be created. You can create a custom object by creating an instance of the Object type and adding properties or methods to it. As follows:
Var o = new Object (); var o = new Object; / / is valid, but this is not recommended; each instance of Object has the following properties and methods:
Constructor: holds the function used to create the current object.
HasOwnProperty (propertyName): used to check whether a given property exists in the current object instance (rather than in the actual prototype).
IsPrototypeOf (object): used to check whether the incoming object is the prototype of the current object.
PropertyIsEnumerable (propertyName): used to check whether a given property can use the for-in statement
ToLocaleString (): returns a string representation of the object that corresponds to the locale of the execution environment
ValueOf (): returns a string, numeric, or Boolean representation of an object. It is usually the same as the return value of the toString () method
ToString (): returns a string representation of the object
Because Object is the foundation of all objects in JavaScript, all objects have these basic properties and methods.
After reading this article, I believe you have a certain understanding of "what are the basic data types in JavaScript". If you want to know more about it, you are 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.
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.