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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Practical PHP Tips: Delete the Last Semicolon in the Code When writing PHP code, you often encounter situations where you need to delete the last semicolon in the code. This may be because copy-pasting introduces extra semicolons, or to optimize code style and structure. In this article, we will introduce some methods to remove the last semicolon in PHP code and provide specific code examples. Method 1: Use the substr function The substr function can return a substring of a specified length from a string. we can

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
