I. JS Variable Declaration, Data Type and Variable Conversion
1.js variable declaration keyword: var
Note: a: js variables are case sensitive;
In b: js, double quotation marks or single quotation marks can be used for strings.
The same name variable can be declared in c: js. The console will not report errors, but the later variable will override the previous one.
2. Classification of data types
a: number (number type)
b: string (string type)
c: boolean (boolean type)
d: null
e: undefined (undefined)
f: object (object type)
Note: NaN is a digital type, but its essence is not a number; it is not equal to all values, including itself.
3. Conversion of JS variables
a: Convert the data type to the digital type using number () function
b: Use Boolean () function to convert data type to boolean type
Note the use of js variables:
Variable names are best defined, starting with letters or underscores, followed by numeric underscores.
a: var age= 12;
b: var test5 ='hello';
c: var _test = 'hello';
2. Three ways of introducing js and checking whether browser javascript supports or not
1. Internal introduction
<script>
......
</script>
2. External introduction (recommended)
<script src="Route" type="text/javascript"></script>
3. Intra-line introduction
<a href="javascript:confirm('aaa');">666</a> <p onclick="javascript:alert('hello');">clickme</p>
4. Check whether the browser supports javascript usage (generally supported by computers)
<noscript> Your browser does not support JavaScript </noscript>
3. Hump Marking and Underline and js
a. Hump marking and underscoring
var firstName = 'king'; //Small hump
var FirstName = 'queen'; //Great Hump
var first_name = 'fyh'; //Underline method
Three kinds of bullet-frame modes of b and js
Warning frame: alert (');
Confirmation box: confirm('');
Tip box: prompt('prompt statement','input box');
Operators in js
1. Arithmetic operators (+,-,*,/,+,--,%)
<script> var i=1,j=2; document.write(i+j); //3 document.write('<br/>'); document.write(i-j); //-1 document.write('<br/>'); document.write(i*j); //2 document.write('<br/>'); document.write(i/j); //0.5 document.write(i%j); //2 document.write('<br/>'); document.write(1%-2); //1 document.write('<br/>'); document.write(-1%2); //-1 document.write('<br/>'); document.write(-2%1); //0 </script>
a:"%" remainder operator, divide the two numbers and take the remainder.
a=10%2; //a=0
b:"+" self-incremental operator
var a=10;
//Add 1 after assignment.
var b=a++;
//The value of a variable is assigned to b, b=10, and then a+1, a=11.
var c=++a;
//The value of variable a is + 1, a=12, and then variable a is assigned to variable c, c=12.
document.write("b="+b+",c="+c);
c:"- -" self-decreasing operator
var num1=2;
alert(- -num1); output num1=1;
// Floating Point Supports Self-Increasing and Self-Decreasing Operators
num1=12.3;
alert (- num1); / / output num1=11.3;
Note: String types do not support self-increasing and self-decreasing operators
2. Assignment operators (=, +=, -=, *=, /=)
"+=": First add and then wait, such as a+=5, expand and then a=a+5;
"-=": First subtract, then wait, such as a-=5, expand and then a=a-5;
"*=": First multiply, then wait, such as a*=5, then expand to a=a*5;
"/=": First divide and then wait, such as a/=5, expand and then a=a/5;
3. String operator (+)
String operators are used only as connectors, and no other operations are performed.
eg: var a='fyh';
var b=a+'666'; //b='fyh'+'666'='fyh666';
4. Comparing operators (>,<,>=,<=,<=,=,! = ====,! = =)
eg: a=5>6; //a=false;
a=5>=6; //a=false;
"==", value comparison, value return true, otherwise return flase;
"===", value and type are compared at the same time, the same return true, different return flase;
"! == "Not absolutely equal, one value or type is not equal, or both are not equal;
"===", absolutely equal, equal in value and type;
5. Logical Operators (&, |,!)
&& (and): Return true only when both sides are established, and false if they are different;
|| (or):a: the first is ture, and the second is false or ture, which returns the previous value.
b: The front is false, and the back, whether it's ture or false, returns the back value.
6. Comma Operator
var a,b,c;
var n=1,m=2;
var z=(x=3,y=4);
console.log(z);//4
console.log(x);//3
7. Ternary Operator (Trinomial Operator) (a > b?'true':'false')
if(3>1){ document.write('aa'); }else{ document.weite('bb'); } document.write('<br/>'); var res=3>1?'aa':'bb'; document.write(res); document.write('<br/>');
8. void operator
<script type="text/javascript"> //void operator z=void(n=1,m=2,p=3); alert(z); //z Output is 3 var x; x=123; x='king'; x=true; x=null; x=undefined; x=[1,2,3]; alert(typeof x); //x The output is undefined </script>
V. Comparison of null, NaN, "" and undefined
Undefined: undefined type
var a;
alert(a); //undefined not defined;
2. null (null value)
alert(typeof null); //object
3. NaN(NaN is a digital type, but its essence is not a number; it is not equal to all values, including itself;)
alert(NaN==NaN);//false;
4, " "
0==" "; //true
0==false; //true
null==undefined; //true
0==undefined; //false
undefined==false; //false
" "==undefined; //false
0==null; //false
null==false; //false
" "==null; //false