Home Web Front-end JS Tutorial Conditional judgment in javascript_basic knowledge

Conditional judgment in javascript_basic knowledge

May 16, 2016 pm 07:18 PM

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles