All things in JS are objects: strings, numbers, dates, and so on;
In JS, the object is the data that owns the property and method
Local JS variable
A variable declared inside a JavaScript function (using VAR) is a local variable, so it can only be accessed inside the function. (The scope of the variable is local).
You can use local variables with the same name in different functions, because only the function that declares the variable can recognize the variable.
The local variable is deleted as soon as the function is finished running
Global JS Variable
Variables declared outside of a function are global variables, and all scripts and functions on a Web page can access it.
The lifetime of the JS variable
The lifetime of JavaScript variables begins at the time they are declared.
Local variables are deleted after the function is run.
Global variables are deleted after the page is closed.
Assigning a value to an undeclared JS variable
If you assign a value to a variable that has not been declared, the variable is automatically declared as a global variable.
This statement:
Carname= "Volvo";
A global variable is declared carname, even if it executes within the function.
JS operator
comparison Operators
Comparison operators are used in logical statements to determine whether a variable or value is equal.
Given x=5, the following table explains the comparison operators:
operator |
Description |
Example |
== |
Equals |
X==8 to False |
=== |
Congruent (value and type) |
X===5 is true;x=== "5" is false |
!= |
Not equal to |
X!=8 is True |
> |
Greater than |
X>8 to False |
< |
Less than |
X<8 is True |
>= |
Greater than or equal to |
X>=8 to False |
<= |
Less than or equal to |
X<=8 is True |
logical operators
Logical operators are used to determine the logic between variables or values.
Given X=6 and y=3, the following table explains the logical operators:
operator |
Description |
Example |
&& |
and |
(x < ten && y > 1) True |
|| |
Or |
(x==5 | | y==5) is false |
! |
Not |
! (x==y) is true |
Conditional operator (trinocular operator)
In JS, in addition to If. else and Swich statements, and a unique three-mesh operator? ... :
Grammar:
Conditions? STATEMENTA:STATEMENTB;
The above statement, first judge the condition condition, if the result is true to execute the statement Statementa, otherwise execute the statement statementb.
It is notable that the JavaScript script interpreter will be semicolon "; "As the Terminator of the statement, both the Statementa and the STATEMENTB statements must be single statements, and the use of multiple statements will give an error.
Example:
<!doctype html>
Add an example of timing
<!doctype html>
JS base 2js object, Operator date (), getHours ();