03JavaScript basics
1. Grammar
1. Case sensitive
Everything in ECMAScript, including variables, function names, and operators are case sensitive
2. Identifier
The so-called identifier refers to the name of a variable, function name, attribute, or function parameter.
Identifier naming rules:
-
The first character must be a letter, underscore (_) or dollar sign ($)
-
Other characters can be letters, underscores, dollar signs or numbers
-
Cannot use keywords, reserved words, true, false, and null as identifiers
3. Annotation
ECMAScript uses C-style comments, including single-line comments and block-level comments
-
Single line comment //
-
Multi-line comment (block-level comment) /* The commented content*/
4. Direct quantity (literal)
All direct quantities (literal quantities) are the data values directly displayed in the program
100 //digital literal'front- end development' //string literal false //Boolean literal /js/gi //regular expression literal null //object literal
2. Keywords and reserved words
1. Keywords
ECMAScript-262 describes a set of keywords with a specific purpose, generally used to control the beginning or end of a sentence, or to perform specific operations, etc.
ECMAScript keywords
break、else、new、var、case、finally 、return 、void、catch、for、switch、while、continue、 function、this、with、default、if、throw、delete、in、try、do、instanceof、typeof等
2. Reserved words
ECMAScript-262 describes another set of reserved words that cannot be used as identifiers. Although reserved words have no specific purpose in JavaScript, they are likely to be used as keywords in the future.
ECMAScript reserved words
abstract、enum、int、short、boolean、export、interface、static、byte、extends、long、 super、char、final、native、synchronized、class、float、package、throws、const、goto、 private、transient、debugger、implements、protected、volatile、double、import、public等
Three, variables
ECMAScript variables are loosely typed, the so-called loose type is used to store any type of data
When defining a variable, use the var operator (var is the key), followed by a variable name (variable name is an identifier)
<script type="text/javascript"> var company; alert(company); //alter pops up a pop-up box in the browser </script>
The so-called variable is the amount that can be changed again after initialization. ECMAScript is a weakly typed (loosely typed) language, which can change the amount of different types at the same time
<script type="text/javascript"> var company ='IT education'; var company ='front-end development'; alert(company); </script>
Note: Although it is possible to change the amount of different types, it brings difficulties for later maintenance, and the performance is not high, resulting in high costs
Four, data type
1. Introduction to data types:
There are five simple data types in ECMAScript, one complex data type
ECMAScript does not support any mechanism for creating custom types. All values in JavaScript should be one of the following six types:
-
undefined
-
null
-
boolean
-
number
-
string
-
object (complex type)
2.typeof operator
The typeof operator is used to detect the data type of a variable. Using the typeof operator for a value or variable will return the following string:
-
undefined #Undefined
-
boolean #Boolean value
-
string #string
-
number #number
-
object #object or null
-
function #function
The typeof operator can manipulate variables and literals, typeof is an operator rather than a built-in function
Note: Function is an object in ECMAScript, not a data type
Therefore, it is necessary to use typeof to distinguish function and object
3.undefined type
The undefined type has only one value, the special undefined
When using var to declare a variable but not initializing it, the value of this variable is undefined
4.Null type
The null type is a data type with only one value, that is, the special value null, which represents a null object reference (pointer), and the typeof operator detects null and returns object
Note: undefined is derived from null
5.boolean type
The boolean type has two values: true and false, and true is not necessarily equal to 1, and false is not necessarily equal to 0
JavaScript is case sensitive, True and False or other values that are not boolean type
Although there are only two types of boolean, true and false, the values of all types in ECMAScript are equivalently converted values of true or false.
To convert a value to its corresponding boolean value, you can use the transformation function boolean() <also known as coercion>
Boolean implicit conversion
The conditional judgment in the if conditional statement is the implicit conversion of the village
Rules for converting other types into Boolean types
type of data | Convert to TRUE value | Convert to FALSE value | |
---|---|---|---|
boolean | true | false | |
string | Any non-empty string | Empty string | |
number | Any non-zero numeric value (including infinity) | 0 sum NaN | |
object | Any object | null | |
undefinde | undefined |
6.number type
The number type contains two types of values: integer type and floating point type
Note: ECMAScript will automatically convert floating-point values that can be converted to integers into integers
NaN
NaN is not a number (Not a Number) is a special value, this value is used to indicate that an operand that originally returned a value does not return a value (this will throw an error)
ECMAScript provides the isNaN() function to determine whether the value is NaN
After the isNaN() function receives a value, it will try to convert the value to a number
7.string type
The string type is used to represent a character sequence composed of zero or more 16-bit Unicode characters, that is, a string
Strings can be represented by double quotes or single quotes, quotes cannot appear in pairs
ECMAScript strings are immutable. Once the strings are created, their values cannot be changed.
To change the string stored in a variable, first destroy the original string, and then fill the variable with another string containing the new value
8.Object type
Objects in ECMAScript are 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 obj = new Object();
Parameters can be passed in Object(), which can be values, strings, Boolean values, etc., and related calculations can also be performed
Five, operator
ECMAScript describes a set of operators used to manipulate data values, including:
-
Unary operator
-
Arithmetic Operator
-
Relational operator
-
Logical Operators
-
Ternary operator
-
.....
Operators in ECMAScript are applicable to many values, including strings, numbers, booleans, objects, etc.
1. Unary operator
Operators that can only operate on one value are called unary operators
Increment ++ and decrement——
2. Arithmetic operators
ECMAScript defines five arithmetic operators:
Addition, subtraction, multiplication, division, modulo (take remainder) <+, -, *, /, %>
3. Relational operators
The operators used for comparison are called relational operators:
Less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equal to (==),
Not equal (!=), congruent (identity) (===), inconsistency (non-identity) (==)
When the relational operator operates on non-numeric values, the following rules must be followed:
-
Both operands are numerical values, then numerical comparison
-
Both operands are strings, then compare the character encoding values corresponding to the two strings
-
One of the two operands is a value, then the other is converted to a value, and then the value is compared
-
If one of the two operands is an object, first call the valueof() method or tostring() method, and then use the result to compare
4. Logical operators
Logical operators are usually used for Boolean operations, and are generally compared with relational operators
There are three logical operators:
-
AND (logical AND)<&&>
-
OR (logical OR)<||>
-
NOT (logical not)<!>
5. Ternary operator
The ternary operator is actually a short form of if statement
<script type="text/javascript"> var box = 5> 4?'right':'wrong'; //right, 5>4 returns true to assign'right' to box, and vice versa. </script>
Expression 1> Expression 2? Result 1: Result 2
-
If expression 1> expression 2 then return result 1
-
If expression 1 <expression 2 then return the result 2s