Home > Article > Web Front-end > The humble basics of javascript, values and semicolons
Value
Sometimes I wonder how the JavaScript parsing engine distinguishes the value of a variable, such as the following code.
var x = 'javascript'; //javascript x = "hello"; // hello x = 555; //555 x = null; //null x = a; //a is not defined x = true; //true
Numbers are assigned directly, because there is no diversity, numbers are numbers. But it is difficult to distinguish when the value is in English, because in a programming language, English may be a string or another variable referenced. Therefore, how to distinguish variables and strings is particularly important. Programming languages often enclose strings in quotation marks to distinguish 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 a string. But JavaScript does not distinguish between characters and strings, but treats them both as strings, so there is no difference between single quotes and double quotes in JavaScript.
Although quotation marks can be used to distinguish variables and strings, the value may often be a keyword. For example, in the above code, I assigned x to null, so how do these programming languages distinguish between variables and strings? What about keywords?
null = 123; console.log(null); //Uncaught ReferenceError: Invalid left-hand side in assignment undefined = 456; console.log(undefined); //undefined
Above I assigned null and undefined to another value respectively. As a result, an error was reported when assigning a value to null. Although no error was reported when assigning a value to undefined, No success either. Maybe for null and undefined, they are the values. Variables look for values. When we talk about how JavaScript distinguishes variables and keywords, it may eventually become how JavaScript distinguishes variables and values.
Semicolon
In some JS plug-ins, you often see a line of code like the following
;(function(){ ......... })();
There is a semicolon at the beginning of the code, so what is this semicolon used for?
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, so a problem arises. The end of the code is not decided by you but by the program. It is decided by the program, and the program is not omnipotent. Often it just follows a certain rule. If the code you write does not conform to its rules, the final result will be somewhat unsatisfactory.
The following are the javascript parsing rules for omitting semicolons
var a = 1 + 2 console.log(a) //3
The javascript parser will parse the above code into
var a = 1 + 2; console.log(a); //3
If javascript does not add a semicolon after 2, it will not be able to parse it. It can also be said that if it cannot be parsed, the javascript parser will try to add a semicolon to it. number, if it still cannot be parsed, an error will be reported. Another example is the following code
var a = 10; var b = 5; var c = a + b (a + b).toString() // b is not a function
It says that b is not a function, which means that the above code is likely to be parsed into the following code
var a = 10; var b = 5; var c = a + b(a + b).toString();
It treats () as a function call. It can also be understood that the JavaScript parser will match as many matches as possible, but there are a few exceptions, which are retrun, break, and continue. When the JavaScript parser parses these keywords, it will not change the content after line breaks. Treat it as its own, but add a semicolon directly before the line break. You might as well look at the following code
function test(){ return 123; } console.log(test()); //undefined
It does not return 123, that is to say It adds a semicolon directly after retrun.
Looking back, why do those plug-in developers add a semicolon in the first line of code?
Since it is a plug-in, it is naturally for others to use, right? But the key problem is that you don’t know how the people who use this plug-in write its code. This seems to be quite a fallacy. Its code and What do we have to do with each other?
If the user's code affects our code, then how does it affect it? For example, we are writing a piece of code similar to the following
<script src="test.js"></script> <script src="zmz.js"></script>
The first script is written by the user himself, and the second script is a plug-in introduced , so how does the browser parse these two scripts? Let’s test it
test.js
var a a
zmz.js
(1+2)
If you run it, you will find that no error is reported, which means that the javascript parser will not This file does not have a semicolon and is parsed together with the code in the latter file.
The problem is not here, but maybe you just read a book about HTTP. Wow, it turns out that merging files can reduce the number of requests, so the two scripts are integrated into one. It turned into something like this
var a a(1+2)
Do you think this can be done without error? If we add a semicolon at the beginning of the plug-in, this kind of thing will not happen. may appear.
var a a;(1+2)
So don’t think of the semicolon as just used to end a certain piece of code, it can also be used to isolate a certain piece of code and draw a clear line between others.
For more articles related to the humble foundation of javascript, values and semicolons, please pay attention to the PHP Chinese website!