国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end JS Tutorial Understanding JavaScript Operators: From Arithmetic to Ternary

Understanding JavaScript Operators: From Arithmetic to Ternary

Jan 25, 2025 pm 12:31 PM

<p>An in-depth exploration of JavaScript operators: from arithmetic operators to ternary operators</p> <p>This article will take an in-depth look at JavaScript operators. </p> <p>JavaScript operators include: </p> <ol> <li>Arithmetic operators: <code> </code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>, <code>**</code> </li> <li> Negation operator: <code>-</code> (unary negative sign), <code>!</code> (logical not) </li> <li>Comparison operators: <code>></code>, <code><</code>, <code>>=</code>, <code><=</code>, <code>==</code>, <code>!=</code>, <code>===</code>, <code>!==</code></li> <li> Logical operators: <code>&&</code> (AND), <code>||</code> (OR), <code>!</code> (not) </li> <li>Null value coalescing operator: <code>??</code></li> <li>Ternary operator: <code>? :</code></li> </ol> <p><strong>True and false values:</strong></p> <p>False values: 0, "" (empty string), <code>false</code>, <code>null</code>, <code>undefined</code>, and <code>NaN</code> True value: all other values ??</p> <p><strong>Operations:</strong></p> <p>The value or variable that the operator acts on. For example, in the expression <code>5 2</code>, the numbers 5 and 2 are the operands. </p> <p><strong>Unary operator:</strong></p> When the <p> operator acts on a single operand, it is called a unary operator. For example: <code> 5</code>, <code>-x</code> (negates x), or <code>typeof "123"</code>. </p> <p><strong>Binary operator:</strong></p> When the <p> operator acts on two operands, it is called a binary operator. For example: <code>5 2</code>, <code>x * y</code>. </p> <p><strong>Arithmetic operators:</strong> <code> </code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>, <code>**</code></p> <p>JavaScript will implicitly convert types during operations. For example: </p> <p><code>5 - "3"</code> The result is the integer 2 ("3" is converted to the integer 3 for subtraction) <code>10 % "3"</code> The result is an integer 1</p> <p><strong><code> </code> Special behavior of operator: </strong></p> <p>For strings, the <code> </code> operator will concatenate them. For example: </p> <p><code>let value = "hello" "world" "!";</code> // "helloworld!"</p> <p>This behavior is different from other arithmetic operators. If either operand is of type string, the <code> </code> operator concatenates the operands. For example: </p> <p><code>"10" 4</code> The result is the string "104". <code>1 1 "5"</code> The result is the string "25" (counted from left to right). </p> <p><strong>One dollar <code> </code>:</strong></p> <p>The unary <code> </code> operator converts non-numbers to numbers. It is short for <code>Number()</code>. For example: </p> <p><code> "123"</code> // 123 <code> true</code> // 1 <code> ""</code> // 0</p> <p><strong> Negation operator: </strong></p> <p>The unary <code>-</code> operator negates a number or variable, effectively flipping its sign. For example: </p> <p><code>let x = 3;</code> <code>console.log(-x);</code> // -3</p> <p><strong>Comparison operator:</strong></p><p> Comparison operators compare numbers, which is the same as what we know from mathematics. For string, JavaScript is compared based on the order of letters or phrases. You can refer to the character code table (ASCII or Unicode). When performing comparison, JavaScript will convert the value to numbers as needed. For example: </p> <p> </> // true <code>"2" > 1</code> JavaScript has some characteristics in its comparative behavior: <p> </p> > // true <p> // false <code>null == undefined</code> // false (nan is defined as "not equal to any value, including itself") <code>null === undefined</code> <code>NaN == NaN</code> Logical operator: </p> <p> <strong> </strong> (and) </p> <p> <strong> The operator returns <code>&&</code> only when both operations are the real values. </strong> </p> > // true <p> // false <code>&&</code> // false <code>true</code> // false </p> <p> JavaScript calculates the value from left to right. If the number of left operation is the real value, continue to calculate the number of right operations and return the right value. <code>true && true</code> <code>true && false</code> // 2 <code>false && true</code> // 3 <code>false && false</code> If the fake value is encountered, stop and return the fake value. </p> <p> > // null </p> // undefined <p> <code>1 && 2</code> If all the operations are true value, return the last value. <code>1 && true && 3</code> </p> // 3 <p> </p> <p> (or) <code>1 && null</code> <code>1 && 2 && undefined</code> </p> If the number of operators is the real value, it returns <p>. </p> <p> > // true <code>1 && 2 && 3</code> // true </p> // true <p> // false <strong> <code>||</code> JavaScript calculates the value from left to right. It returns the first true value it encountered. </strong> </p> > // 1 <p> // 2 <code>||</code> If the number of operations is true, the last value is returned. <code>true</code> </p> // 0 <p> <code>true || true</code> <code>true || false</code> (Non) <code>false || true</code> <code>false || false</code> </p> Bur value of the number of operators in the operation character. It converts the number of operations to the Boolean value (<p> or </p>), and then returns the opposite value. <p> <code>null || 1</code> > // false <code>undefined || 0 || 2 || 5</code> // true </p> <p> </p> <p> The air value merging operator: <code>null || undefined || 0</code> </p> <p> <strong> The empty value combined computing character <code>!</code> is a newer operator (ES2020). It helps to deal with whether the check value is </strong> or </p> and return the default value. <p> <code>!</code> grammar: <code>true</code> <code>false</code> If is not </p> or <p>, then <code>!true</code>. If <code>!0</code> is or <code>if (!true) {</code>, then <code>console.log("This won't run.");</code>. <code>}</code></p> <p> This operator is useful because it handles specifically <code>null</code> or <code>undefined</code> and is not affected by false values ??such as <code>0</code>, <code>""</code> or <code>false</code>, whereas the <code>||</code> (or) operator Treats them as false and replaces them with the second value. </p> <p><code>let height = 0;</code> <code>alert(height || 100); // 100 (因?yàn)?0 是假值)</code> <code>alert(height ?? 100); // 0 (因?yàn)?0 不是 null 或 undefined)</code></p> <p><code>||</code> Returns the first true value <code>??</code> Returns the first defined value </p> <p><strong>Ternary operator: <code>? :</code></strong></p> <p>Syntax: <code>let result = condition ? value1 : value2;</code></p> <p>The condition is evaluated. If true, returns <code>value1</code>; otherwise, returns <code>value2</code>. </p> <p>The ternary operator is concise and clear, and is usually used for simple conditional assignment. For example: </p> <p><code>const className = isActive ? "red" : "blue";</code></p> <p>However, it is not recommended to use the ternary operator to execute different blocks of code (such as <code>if</code> statements). This results in less readable code. </p> The <p><code>||</code> and <code>??</code> operators sometimes appear to produce similar results, but they cannot be used interchangeably. The <code>&&</code> operator returns the first false value it encounters, while the <code>??</code> returns the second value only if the first value is <code>null</code> or <code>undefined</code>. Depending on the behavior you want to achieve, it's important to carefully consider which operator to use. </p> <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173777947349716.jpg" class="lazy" alt="Understanding JavaScript Operators: From Arithmetic to Ternary"></p> </li> </ol>

The above is the detailed content of Understanding JavaScript Operators: From Arithmetic to Ternary. For more information, please follow other related articles on the PHP Chinese website!

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Java vs. JavaScript: Clearing Up the Confusion Java vs. JavaScript: Clearing Up the Confusion Jun 20, 2025 am 12:27 AM

Java and JavaScript are different programming languages, each suitable for different application scenarios. Java is used for large enterprise and mobile application development, while JavaScript is mainly used for web page development.

Javascript Comments: short explanation Javascript Comments: short explanation Jun 19, 2025 am 12:40 AM

JavaScriptcommentsareessentialformaintaining,reading,andguidingcodeexecution.1)Single-linecommentsareusedforquickexplanations.2)Multi-linecommentsexplaincomplexlogicorprovidedetaileddocumentation.3)Inlinecommentsclarifyspecificpartsofcode.Bestpractic

How to work with dates and times in js? How to work with dates and times in js? Jul 01, 2025 am 01:27 AM

The following points should be noted when processing dates and time in JavaScript: 1. There are many ways to create Date objects. It is recommended to use ISO format strings to ensure compatibility; 2. Get and set time information can be obtained and set methods, and note that the month starts from 0; 3. Manually formatting dates requires strings, and third-party libraries can also be used; 4. It is recommended to use libraries that support time zones, such as Luxon. Mastering these key points can effectively avoid common mistakes.

Why should you place  tags at the bottom of the ? Why should you place tags at the bottom of the ? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript vs. Java: A Comprehensive Comparison for Developers JavaScript vs. Java: A Comprehensive Comparison for Developers Jun 20, 2025 am 12:21 AM

JavaScriptispreferredforwebdevelopment,whileJavaisbetterforlarge-scalebackendsystemsandAndroidapps.1)JavaScriptexcelsincreatinginteractivewebexperienceswithitsdynamicnatureandDOMmanipulation.2)Javaoffersstrongtypingandobject-orientedfeatures,idealfor

JavaScript: Exploring Data Types for Efficient Coding JavaScript: Exploring Data Types for Efficient Coding Jun 20, 2025 am 12:46 AM

JavaScripthassevenfundamentaldatatypes:number,string,boolean,undefined,null,object,andsymbol.1)Numbersuseadouble-precisionformat,usefulforwidevaluerangesbutbecautiouswithfloating-pointarithmetic.2)Stringsareimmutable,useefficientconcatenationmethodsf

What is event bubbling and capturing in the DOM? What is event bubbling and capturing in the DOM? Jul 02, 2025 am 01:19 AM

Event capture and bubble are two stages of event propagation in DOM. Capture is from the top layer to the target element, and bubble is from the target element to the top layer. 1. Event capture is implemented by setting the useCapture parameter of addEventListener to true; 2. Event bubble is the default behavior, useCapture is set to false or omitted; 3. Event propagation can be used to prevent event propagation; 4. Event bubbling supports event delegation to improve dynamic content processing efficiency; 5. Capture can be used to intercept events in advance, such as logging or error processing. Understanding these two phases helps to accurately control the timing and how JavaScript responds to user operations.

What's the Difference Between Java and JavaScript? What's the Difference Between Java and JavaScript? Jun 17, 2025 am 09:17 AM

Java and JavaScript are different programming languages. 1.Java is a statically typed and compiled language, suitable for enterprise applications and large systems. 2. JavaScript is a dynamic type and interpreted language, mainly used for web interaction and front-end development.

See all articles