In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "JavaScript basic grammar and data type case analysis". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "JavaScript basic grammar and data type case analysis".
1. JavaScript syntax 1. Case-sensitive
Everything in ECMAScript, including variables, function names, and operators, is case sensitive. For example, text and Text represent two different variables.
2. Identifier
The so-called identifier refers to the name of a variable, function, attribute, or the parameter of a function. Identifiers can be one or more characters combined in the following formats:
A, the first character must be a letter, an underscore (_) or a dollar sign ($).
B, other characters can be letters, underscores, dollar signs, or numbers.
Keywords, reserved words, true, false and null cannot be used as identifiers.
For example: myName, Book123, etc.
3. Notes
ECMAScript uses C-style comments, including single-line comments and block-level comments.
Example:
/ / single-line comment / * this is a multi-line * comment * / 4, variable
The variables of ECMAScript are loosely typed, which is used to hold any type of data. Define variables using the var operator (var is the key), followed by a variable name (the variable name is an identifier)
Var box declares variables
Var box=100 declares variables and initializes
2. The data type of JavaScript
There are five simple data types in ECMAScript: Undefined, Null, Boolean, Number, and String. There is also a complex data type-Object. ECMAScript does not support any mechanism for creating custom types, and all values are one of the above six data types.
1. Undefined type
The Undefined type has only one value, undefined.
Indicates that the definition is not defined or that the definition is not assigned.
Look at the following example:
JavaScript data type / / 1, Undefined / / No alert (a) is defined; / / the page will report an error:
Effect:
JavaScript data type / / 1, Undefined / / undefined / / alert (a); / / page will report an error: / / define unassigned var box; alert (box); / / pop-up alert box: undefined
Effect:
There is no need to explicitly assign a variable to undefined, because variables that are not assigned will be implicitly assigned to undefined; while the main purpose of undefined is for comparison. This value was not introduced before the third edition of ECMAScript, and was introduced to formally distinguish between empty objects and uninitialized variables.
Note: uninitialized variables are also different from variables that do not exist (undeclared variables).
Var box;alert (age); / / age is not defined
If all typeof box,typeof age returns the undefined. Logically, their values, one is undefined, the other is error; their types, however, are all undefined. Therefore, when we define variables, as far as possible, do not just declare, do not assign values.
Var box;alert (typeof box); / / box is the Undefined type, the value is undefined, and the string returned by the type is "undefined". 2. Null type
The Null type is a data type that has only one value, that is, the special value null. It represents an empty object reference (pointer), and the typeof operator detection null returns Object.
Look at the following example:
JavaScript data type / / 1, Undefined / / undefined / / alert (a); / / page error: / / definition unassigned var box; / / alert (box); / / pop-up alert box: undefined / / 2, Null var box=null; alert (typeof box)
Effect:
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. Like this. When you check the null value, you will know whether the variable has been assigned an object.
Var box=null;if (bodily invalid null) {alert ('box object already exists!') ;} 3, Boolean type
The Boolean type has two values: true and false. While true does not have to be equal to 1 Boolean false or 0 to 0 JavaScript is strictly case-sensitive, True and False or other values are not of type Boolean.
Look at the following example:
JavaScript data type / / 1, Undefined / / undefined / / alert (a); / / page error: / / definition unassigned var box; / / alert (box); / / pop-up alert box: undefined / / 2, Null / * var box=null; alert (typeof box) * / / 3, Boolean var box=true; alert (box)
Effect:
Note:
Although the literals of the Boolean type are only true and false, all types of values in ECMAScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can use the transformation function Boolean ().
Var hello='Hello World';var hello2=Boolean (hello); alert (typeof hello); pop-up true
The above is a display conversion, which is a mandatory conversion. In practical application, there is also a kind of implicit conversion. For example, for conditional judgment in if conditional statements, there is an implicit conversion.
Var hello = 'Hello WorldSomething' if (hello) {alert ('if the condition is true, execute mine!') ;} else {alert ('if the condition is false, execute mine!') ;} var box=true;alert (typeof box); / / box is a Boolean type, the value is true, and the strings returned by the type are boolean4 and Number
The Number type contains two types of values: integer and floating point.
The range of floating point values: between Number.MIN_VALUE and Number.MAX_VALUE.
Alert (Number.MIN_VALUE); / / minimum
Alert (Number.MAX_VALUE); / / maximum
Look at the following example:
JavaScript data type / / 1, Undefined / / undefined / / alert (a); / / page error: / / definition not assigned / / var box; / / alert (box); / / pop-up alert box: undefined / / 2, Null / * var box=null; alert (typeof box) * / / 3, Boolean / * var box=true; alert (box); * / / 4, Number var box=45; / / integer var box1=45.6;// floating point alert ("box value is" + box+ "," + "box1 value is" + box1 ")
Effect:
Note:
In addition to integer and floating-point types, there is another type of Number type: NaN, or Not a Number, is a special value that represents an Operand that is supposed to return a numeric value but not a numeric value (so that an error is not thrown). For example, in other languages, any number divided by 0 will cause an error and terminate program execution. However, in ECMAScript, a special value is returned, so it does not affect program execution.
For example:
JavaScript data type / / 1, Undefined / / undefined / / alert (a); / / page error: / / definition not assigned / / var box; / / alert (box); / / pop-up alert box: undefined / / 2, Null / * var box=null; alert (typeof box) * / / 3, Boolean / * var box=true; alert (box); * / / 4, Number / * var box=45; / / integer var box1=45.6;// floating point alert ("box value is" + box+ "," + "box1 value is" + box1 "); * / / Special Number type: NaN var box=0/0 / / NaN var box1=12/0; / / Infinity infinity var box2=12/0*0; / / NaN alert ("the value of box is" + box+ ", the value of" + "box1 is" + box1+ ", and the value of" + "box2 is" + box2)
Results:
The nan value can be obtained through Number.NaN, and the result of any operation with NaN is that NaN,NaN is not equal to itself (NaN is not equal to any value).
Alert (Number.NaN); / / NaNalert (NaN+1); / / NaNalert (NaN==NaN); / / false
ECMAScript provides the isNaN () function to determine whether the value is NaN or not. After receiving a value, the isNaN () function attempts to convert the value to a numeric value.
Alert (isNaN (NaN)); / / truealert (isNaN (25)); / / false 25 is a number
The isNaN () function also applies to objects. During the call to the isNaN () function, the valueOf () method is called first, and then it is determined whether the return value can be converted to a numeric value. If not, the toString () method is called based on this return value, and the return value is tested.
Var box= {toString:function () {return '123clients;}}
There are three functions that convert non-numeric values to numeric values: Number (), parseInt (), and parseFloat (). The Number () function is a transformation function that can be used for any data type, while the other two are used specifically to convert strings to numeric values.
Alert (Number (true)); / 1 the true and false of the false Boolean type are converted to 1 and 0alert (Number (25)), respectively; / / 25, the numeric type directly returns var box=250;alert (typeof box); / / box is the Number type, the value is 250, and the string returned by the type is number. 5. String type
The String type is used to represent a sequence of zero or more 16-Unicode characters, that is, strings. Strings can be represented by double quotation marks (") or single quotation marks (').
For example:
Var box= "I am a string type, I use double quotes"; var box1=' I am also a string type, I use single quotation marks'
Note: in some other languages, single and double quotation marks represent strings differently, but there is no difference between the two representations in ECMAScript. But keep in mind that it must appear in pairs and cannot be interspersed with it, or it will go wrong.
For example:
/ / error will be reported. Single quotation marks and double quotation marks cannot cross-use var box='. I am a string type, I use double quotation marks. "
If the value has a toString () method, the method is called and the corresponding result is returned; if it is null or undefined, "null" or "undefined" is returned.
The var box=' string'; alert (typeof box); / / box is of type String, the value is a string, and the string returned by the type is string. 6. Object type
An object in ECMAScript is actually 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.
Var box=new Object (); III. Typeof operator
The typeof operator is used to detect the return value of a variable, which is a string type. Using the typeof operator for a value or variable returns the following string:
For example:
JavaScript data type / / 1, Undefined / / undefined / / alert (a); / / page error: / / definition not assigned / / var box; / / alert (box); / / pop-up alert box: undefined / / 2, Null / * var box=null; alert (typeof box) * / / 3, Boolean / * var box=true; alert (box); * / 4, Number / * var box=45; / / integer var box1=45.6;// floating point alert ("box value is" + box+ "," + "box1 value is" + box1 "); * / / Special Number type: NaN / * var box=0/0 / / NaN var box1=12/0; / / Infinity infinite var box2=12/0*0; / / NaN alert ("box value is" + box+ "," + "box1 value is" + box1+ "," + "box2 value is" + box2 "); * / / 5, String / * var box=" I am a string type, I use double quotation marks "; var box1=' I am also a string type, I use single quotation marks' * / / will report errors, single quotation marks and double quotation marks cannot be cross-used / / var box=' I am a string type, I use double quotes "; / * var box=" I am a string "; box=" I am also a string "; alert (box); * / typeof operator var box; var box1=23; var box2=true; var box3=null Var box4= "box"; [xss_clean] ("box:" + typeof box + ""); [xss_clean] ("box1:" + typeof box1 + ""); [xss_clean] ("box2:" + typeof box2 + "); [xss_clean] (" box3: "+ typeof box3 +"); [xss_clean] (" box4: "+ typeof box4 +")
Results:
Note:
The typeof operator returns a string.
The typeof operator can manipulate variables as well as literals. Although you can also use typeof (box), typeof is an operator rather than a built-in function.
PS: functions are objects in ECMAScript, not a data type, so it is necessary to use typeof to distinguish between function and Object.
Thank you for your reading, the above is the content of "JavaScript basic Grammar and data Type instance Analysis". After the study of this article, I believe you have a deeper understanding of the problem of JavaScript basic syntax and data type instance analysis. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.