Basic Javascript syntax
1. Relationship between JavaScript and DOM:
The browser has the function of rendering HTML code, which forms a DOM object in the memory, that is, the Document Object.
The browser has a JavaScript interpreter/executor/engine.
Write a JavaScript code in HTML. The JavaScript code is executed by the engine and the result is the DOM operation.
The result of DOM operations is the special effect we often see, the film floats, and the text color changes.
The JavaScript host is not only a browser, but also a server.
The main difference between learning JavaScript is as follows:
(1). the syntax of the JavaScript language itself.
(2). DOM object (regard the node tree such as body, div, and p as an object)
(3). BOM object (add the address bar, history records, DOM, and so on of the browser to an object.
2.
3. Naming rules:
JavaScript variable names can contain _, numbers, letters, $, and cannot start with a number.
Declared variables are declared using the var variable name.
Var a = 34;
Var B = 45;
Alert (a + B );
Console. log (a, B, a + B );
Var $ c = 'World ';
Alert ($ c );
Var $ = 'jquery ';
Alert ($ );
Note: variables in JavaScript must be case sensitive. Str and Str are not variables.
Note: global variables are contaminated without var.
4. // js variable type
Var a = 23; // Integer type
Var B = 3.14; // floating point number type
Var c = hello; // string type
Var d = 'World ';
Var e = true; // bool type
Var f = null; // no object data
Var g = undefined; // no original application data
Var h = {name: 'lisi', age: 20}; // join array, Object Type
Console. log (h. name );
Console. log (h ['name']);
Var arr = ['A', 3, 'Hello', true]; // The array is always incrementing from 0, 1, 2...
Console. log (arr );
Alert (arr );
Console. log (arr [2]);
5. // concatenation operator + in js
Console. log (2 + 3 );
Console. log ('hello' + ''+ 'World ');
Console. log (2 + 3 + 4 + 'hahaha' + 5 + 6); // 9haha56. After an invalid number is encountered, all the subsequent data is directly spliced.
// The logic operation in js returns the first value that can judge the expression result.
Var a = false;
Var B = 6;
Var c = true;
Var d = (a | B | c );
Var e = (a | c );
Console. log (d); // 6
Console. log (e); // true
Console. log (a & B); // flase
6. Control Structure in JavaScript:
Var arr = ['zhao ', 'qian', 'sun', 'lil'];
For (var I = 0; I <arr. length; I ++ ){
Console. log (arr [I]);
}
Var obj = {
Name: 'lisi ',
Age: 29,
Area: 'bj'
};
For (var k in obj ){
Console. log (k + '~ '+ Obj [k] + '~ '+ Obj ['K']);
}
/* Running result
Name ~ Lisi ~ Undefined
Age ~ 29 ~ Undefined
Area ~ Bj ~ Undefined
*/