This article mainly introduces the basic knowledge of javascript-value and semicolon, which has a good reference value. Let's take a look at it together with the small Editor.
Value
Sometimes I want to know how the javascript parsing engine distinguishes the values of a variable, such as the following code.
var x = 'javascript'; //javascriptx = "hello"; // hellox = 555; //555x = null; //nullx = a; //a is not definedx = true; //true
Numbers are directly assigned values because they are not diverse. Numbers are numbers. However, it is difficult to differentiate values in English, because in programming languages, English can be either a string or another referenced variable. Therefore, how to distinguish between variables and strings is particularly important. Programming Languages often enclose strings in quotation marks to distinguish between variables and strings. Some languages, such as java, also distinguish between single quotes and double quotes. Single quotes enclose a character, while double quotes enclose strings. However, javascript does not distinguish between characters and strings, but treats them as strings. Therefore, there is no difference between single quotes and double quotes in javascript.
Although quotation marks can be used to distinguish between variables and strings, the value may also be a keyword. For example, in the code above, I assigned the value of x to null, how do these programming languages distinguish between variables and keywords?
null = 123;console.log(null); //Uncaught ReferenceError: Invalid left-hand side in assignmentundefined = 456;console.log(undefined); //undefined
I assigned another value to null and undefined respectively. In the result, an error is returned when assigning null values. Although no error is returned when assigning a value to undefined, It is not successful. For null and undefined, they are values. The variable is the search value. We will talk about how javascript distinguishes between variables and keywords. In the end, it may become how javascript distinguishes between variables and values.
Semicolon
In some JS plug-ins, we often see a line of code similar to the following.
;(function(){ .........})();
There is a semicolon at the beginning of the code. What is the use of this semicolon?
We know that a semicolon represents the end of a piece of code, but the problem is that javascript allows you not to write a semicolon, which leads to a problem, the end of the Code is not determined by you, but by the program, and the program is not omnipotent. Often, it is just a rule, if the code you wrote does not match the rule, the final result will be somewhat unsatisfactory.
The following are the parsing rules for omitting semicolons in javascript:
var a=1 + 2console.log(a) //3
The javascript parser parses the above Code
var a = 1 + 2;console.log(a); //3
If javascript does not add a semicolon to the end of 2, it cannot be parsed. You can also say that if it cannot be parsed, the javascript parser will try to add a semicolon to it, if the parsing fails, an error is returned. For example, the following code
var a = 10;var b = 5;var c = a + b(a + b).toString()// b is not a function
It means that B is not a function, that is to say, the above Code may be parsed into the following code.
var a = 10;var b = 5;var c = a + b(a + b).toString();
It regards () as a function call. It can also be understood that the javascript parser will match as much as possible, but there are also several exceptions, they are retrun, break, and continue. When the javascript parser parses these keywords, it does not regard the content after the line break as its own, but directly adds a semicolon before the line break. Let's take a look at the following code.
function test(){ return 123;}console.log(test()); //undefined
It does not return 123, that is, it directly adds a semicolon to the retrun.
Let's look at it. Why do Plug-In developers Add a semicolon in the first line of code?
Since it is a plug-in, it is natural to use it for others, but the key question is that you do not know how the code of the plug-in is written by the person using it. This seems like a paradox, what is the relationship between its code and ours.
If the user's code affects our code, how does it affect it? For example, we are writing a piece of code similar to the following: