Introduction
JavaScript is a scripting language. (Scripts, text commands. When executed, a system interpreter translates them into machine-recognizable instructions and then executes them. Common scripts: batch scripts, T-SQL scripts, VBScript, etc.)
HTML is just a markup language that describes the appearance of a web page, and does not have the ability to calculate and judge. If all calculations and judgments (such as determining whether the text box is empty or whether the password is entered consistently) are placed on the server side to execute the web page, the page will be very slow and difficult to use, and it will also put a lot of pressure on the server. Therefore, it is required to perform some simple operations and judgments in the browser. JavaScript is a scripting language that is executed on the browser side.
JavaScript and Java have no direct relationship. The only relationship is that JavaScript is originally named LiveScript. Later, it absorbed some features of Java and upgraded to JavaScript. JavaScript is sometimes referred to as JS for short.
JavaScript is an interpreted language that can run at any time without compilation.
JavaScript can be cross-platform, as long as there is a browser that supports JS. (Windows, Linux, Mac, Unix)
Syntax and precautions
1) Case sensitivity: JavaScript is strictly case sensitive. (n and N are two different variables.)
2) Weak type language, use var:var num=10;num=true;
3) Strings are in single quotes: var msg='Still it won't leak'
4) The semicolon after each sentence
5) Js comments are the same as those in C# and Java (//single-line comments (recommended), /* multi-line comments*/ (there will be problems when using regular expressions)).
Dynamic Language
JS is a very flexible dynamic language, not as rigorous as static languages such as C#. The JS completion function in development tools is just an auxiliary and suggestion.
The member calls that come out of "." may not be used, and the member that comes out of "." may also be able to call them, so don't worry about the code problems because of "not coming out". When writing js, you should be clear about what members of the current object are and cannot rely on intelligent prompts.
Dynamic Language: Determines the data type at runtime.
JavaScript is dynamic type, so var i=10;i='variable' is legal.
Reference external JS files
In addition to declaring JavaScript on the page, JavaScript can also be written into a separate js file and then introduced in the page: <scriptsrc="test.js" type="text/javascript"></script>.
The advantage of declaring to a separate js file is that multiple pages can also be shared and reduced network traffic.
Notice:
1) You can write the <script> tags imported into external files at the end of the document to improve the user experience.
2) No need to write <script> in js file
Variable naming rules
1) Start with a letter, underscore or $,
2) The middle can include letters, numbers, underscores or $. (There is one more $ in the variable naming)
3) It is recommended to use single quotes to declare strings
4) Local variables are recommended to be declared with var
Determine whether it has been declared
Methods in JavaScript to determine whether declared variables and parameters are initialized (available):
Assume that there is already a variable x:
The code copy is as follows:
if (typeof(x) !='undefined' && x!=null) { alert("Available"); }
if(x) { alert('Variable available!'); } else { alert('Variable not available!'); } //null, undefined, '', 0 are all considered false
Recommend the second one
No block-level scope exists
The scope of variables declared in blocks such as for, while or if inside the function is also within the entire function. Therefore, in order to avoid confusion, it is better to declare these variables directly at the beginning of the function.
The code copy is as follows:
Var n=10;
function ff(){
n++;
};
Conclusion: There is no block-level scope in JS.