JavaScript is a scripting language that can run in the browser. It is a weak language (compared to C, C#, and JAVA). As long as it is a computer language, conditional judgments will be used, and JavaScript is a "weak" language. "Language, its conditional judgment is often confusing, especially for people who have programming experience in other strong languages, it is even more unreasonable! Don’t make fun of experts and unconfused people. I myself was depressed for a while. Let’s write an example first:
var s = "meizz";
if (s && s==true)
{
alert("Is true");
}
Regardless of whether the result of the above code is correct or not, I assigned the variable s a character type, and in the subsequent if conditional judgment, the variable s is directly written as a judgment expression Yes, if you deal with a character variable in a strong language, you need to either judge whether its type is a character or whether the value of the variable is equal to a certain string. But in JavaScript, there is no type judgment (typeof) and no value. Judgment (==) just sitting there can be used as a conditional judgment expression. Of course, this kind of conditional expression alone is not just a character variable, it can also be a numeric variable, an object, a function or a method, etc. That's what's so confusing about this syntax.
I tested and made statistics: in JavaScript, strings that are not empty "", numbers that are not 0, objects that are not null, object properties that are not undefined, and Boolean true can be used individually as When an expression in a judgment is considered true by JavaScript, otherwise it is considered false.
The reason is that JS scripts do not have strong variable types. If you assign a value in the first sentence var s = "meizz"; in the next sentence you can even write it immediately as s = function(){}; without any type conversion in the middle. These writing methods are inconceivable and even rebellious in strong languages (this syntax is not supported at all), but they are so natural in JS scripts. The reason is that there is no mandatory variable type. The variable type is dynamic, which means that the variable itself has no type, but the value of the variable has a type. Haha, this involves other modules. Well, I will write another article to discuss the variable types.
Having said this, it is not difficult to understand the judgment expression of if (s &&.....For the same reason, I can even function s(){} and then use if (s && .... Or var s = document.getElementById("ObjectId"); if (s && .... This syntax is correct in JS scripts. As for the judgment results, you can refer to the statistical results above.
Now let’s discuss the results of the above few lines of code. The only combined operation of the results of multiple conditional judgment expressions is "and" and "or". As for "not", because it is a unary operator, it only For a single value, such as if (!s)... When there is more than one conditional expression, the result of the conditional expression is only AND and OR. The conditional expression above: if. (s && s==true) is a composite judgment of two conditional expressions. As for the AND operation (as long as one value is false, the value is false) or the operation (as long as one value is true, the value is true). I don’t know the details of these operations. Enough said, the textbook is much better than my pen. Now let’s analyze this judgment: This is an “AND” operation. The first judgment s, because its character value is not "", is certainly considered in JS. It is equivalent to true. The second judgment expression is s==true. Obviously this is not equal and the value is false. Because it is an "AND" operation, of course the result of the entire conditional judgment expression is false, so alert() will not be run.
Third, let me talk about some very special conditional judgments. Without any declaration (var) and assignment, JS will throw an undefined variable error if you directly use it to judge a variable. Come out. For example:
if (ss) alert("The conditional judgment result is true!");
An error will occur when running this code because this variable ss has never been declared and assigned a value. , this situation is not equivalent to the above-mentioned null "" undefined. Where does this situation usually occur? One is that you accidentally fail to declare it when you write the code yourself, and the other is to directly use the ID to operate the controls that are not in the web page. When, such as if (InputId.value!="") ... , if there is no text box with id="InputId" in the web page or the text box has not been loaded by the browser when executing this script operation , then an undefined variable error will be thrown.
The result is: use typeof to judge the first case.if (typeof(ss)=="undefined") alert("Variable is not defined"); In the second case, do not use ID to refer to the object directly but use standard object reference. Example:
var e = document.getElementById("InputId"); //IE used to use document.all.InputId
if (e && e.value!="");// ......
This way this kind of error will not happen.
So some people will say that the above code must be written like this, why not directly
if(document.getElementById("InputId").value != "") ;// ....
Isn’t this code more concise? Although the code has been streamlined, errors have also appeared. As long as there is no such object in the web page or the object has not been loaded when the script is executed, an error will be reported. It turns out that document.getElementById("InputId") returned the null value, and null obviously does not have a value attribute, and my code also used e.value to get the attribute but no error was reported. The reason is that the C series language is Another attribute in multiple conditional expressions: When judging multiple conditional expressions in combination, first look at the first conditional expression. If the condition is met, the second judgment expression will not be judged; that is, in the third judgment expression When a conditional judgment fails to meet the standard, the second judgment will be judged, and so on until the end. if (e && e.value!="") is a combined judgment of two judgments. This is an "AND" operation. As long as one judgment is false, the entire value is false. The first judgment expression e returns null because it does not exist or is not loaded. In JS, null is equivalent to false. In this way, the result of the entire combined judgment can be obtained as false without subsequent judgments, so the system will no longer Let’s judge the following sentence e.value. This is different from the B series languages, so you need to pay special attention to it. In language B, a statement like if e and e.value!="" then is to first perform all the judgments and then perform the combined "AND" operation. So this code is correct in JS, but it may not be correct if it is placed in VBS.
Okay, I don’t have much in my stomach, and my writing is poor, so that’s all I’ve written. I hope everyone can correct me!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


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

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment