, etc.; 4. String operators, <, >, etc.; 5. Logical operators, “&&”, “!”, etc.; 6. Assignment operators, =, “+=", etc."/> , etc.; 4. String operators, <, >, etc.; 5. Logical operators, “&&”, “!”, etc.; 6. Assignment operators, =, “+=", etc.">
search
HomeWeb Front-endJS TutorialHow many JavaScript operators are there?

How many JavaScript operators are there?

Jun 10, 2021 pm 04:36 PM
javascriptoperator

There are six types of JavaScript operators, namely: 1. Arithmetic operators, , -, *, etc.; 2. Identity operators, "==", "===", etc.; 3. Comparison operators, , etc.; 4. String operators, , etc.; 5. Logical operators, “&&”, “!”, etc.; 6. Assignment operators, =, “ = "wait.

How many JavaScript operators are there?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are many operators in JavaScript, mainly divided into arithmetic operators, equality operators, comparison operators, string operators, logical operators, assignment operators, etc. These operators have some own operation rules. Let’s introduce the operators in JavaScript to you.

1. Types of JavaScript operators

1. Arithmetic operators: , - , * , / , % , -(unary negation) , , --

2. Identity operators: ==, ===, !==, !===

3. Comparison operators: , =

4. String operators: , =, =,

5. Logical operators: &&, ||, !,

6. Assignment operators: =, =, *=, -=, /=

2. Rules of JavaScript operators

1. Arithmetic operator rules

" ": "Addition" and "Connection" operations can be performed; if one of the two operators is a string, JavaScript will use the other Convert to a string and then concatenate the 2 operands.

" ": If an operand is an object, JavaScript will convert the object into a number for addition or a string for concatenation;

"-""*""/" if If one of the two operands is a non-number, it is converted into a number to perform mathematical operations.

"/" In JavaScript, since all numbers are floating point numbers, the results of division are all floating point numbers, 5 / 2 = 2.5; the result of dividing by 0 is plus or minus infinity; 0/0 is NaN;

"%" modulo operator: Calculates the modulo of the first operand to the second operand, that is, when the first operand is divided by the second operand, the remainder is returned. If the operand is non-numeric, it is converted to a number.

“-” unary negation: negate the operand. If the operand is not a number, convert it into a number.

“ ”“--” Increment operator/decrement operator symbol: The operand must be a variable, an element of a tree group, or an attribute of an object. If the operand is not a number, it is converted to a number.

Note: If " " is located before the operand, increment the operand first, and then calculate the increased value of the operand.

If "--" is located after the operand, first calculate the value before the operand is incremented, and then increment the operand.

For example: i = 1; //Assign the value 1 to i

j = i; //First increment i to 2, then assign 2 to j, the value of i is 2, the value of j is also 2.

i = 1; //Assign i to value 1

j = i; //Assign i to j first, and then increment i to 2. The value of i is 2. The value of j is 1.

The decrement operator "--" is the same as " ".

2. Equality operator and identity operator

(I) "==" "!==" Equality operator and non-identity operator:

Compares 2 operands and returns a non-Boolean value.

Comparing numerical values, strings, and Boolean values ​​all use quantitative values. Two variables are equal if and only if the values ​​stored in them are equal.

When comparing objects, arrays, and functions, references are used. Two variables are equal only when they refer to the same object. Two different arrays are completely different, even if they have exactly the same elements. For variables that store references to objects, arrays, and functions, they are equal only when they refer to the same object, array, or function.

Attention! Principles to be followed:

When the types of the two operands are different: convert them into the same type,

1) A number and a character String, after converting the string into a number, compare.

2) true is converted to 1 and false is converted to 0 for comparison.

3) An object, array, function and a number or string, the object, array, or function is converted into a primitive type value and then compared. (Use valueOf first, and if that doesn’t work, use toString)

4) Other types of combinations are not equal.

If the two operand types are the same, or after conversion to the same type:

1) Two strings: If the characters at the same position are equal, the two strings are the same.

2) 2 numbers: If the 2 numbers are the same, they are the same. Not the same if one is NaN, or if both are NaN.

3) If both are true, or if both are false, it means the same thing.

4) If two references refer to the same object, function, or array, they are equal. If they refer to different objects, functions, or arrays, they are not the same. Even if these two objects, functions, and arrays refer to Can be converted to an exact equivalent of the original value.

5) 2 nulls, or both are undefined, then they are equal.

"!=" non-equality operator: the opposite result of the equality operator detection.

(II) "===" "!===" identical operator and non-identical operator symbol:

The identical operator follows the comparison rules of the equality operator, but It does not perform type conversion on the operands. When the types of the two operands are different, false is returned; only when the types of the two operands are the same, the comparison rules of the equality operator are followed.

The "!==" non-identical operator has the opposite result than the identical operator. Returns true if the types or values ​​of the two operands are different.

3. Comparison operators

These comparison operators are used to compare values ​​of different types, and the result returns a Boolean value.

“”“=”

Note the rules: The operands for comparison can be of any type, but they can only be used in numerical and operation Execute on numbers. Operands that are not numbers and strings will be converted to numbers or strings.

1) If both operands are numbers, or both can be converted into numbers, the comparison will be based on the size of the numbers;

2) If both operands are strings, Or they can be converted into strings, and they will be compared in alphabetical order;

3) If the string encounters a number, the string will be converted into a number for comparison.

4) If the operand cannot be converted into a number or a string, the result is false.

4. String operators

There are no dedicated string operators, but some operators behave differently when encountering string operands.

(I) " " connects two strings;

1) When both operands are strings, connect them together;

2) When When one of them is a number, convert the numbers into strings and concatenate them;

(II) Comparison operators such as ">" confirm the order of the two strings through comparison, and the comparison uses the order of characters. , the smaller ones go before the larger letters, and the uppercase letters go before the lowercase letters.

(III) The method of " " depends on the calculation order,

For example: s = 1 2 "var" then: return the result 3var; because 1 2 is calculated first, and then the result 3 Convert it into a string and connect it with "var";

For example: s = "var" 1 2 then: return the result var12; because the connection between var and 1 is calculated first, and then the result var1 is converted into a string 2 are connected.

5. Logical operators

are used to perform Boolean operations and are often used together with comparison operators to express complex comparison operations.

"&&" logical AND operation, "||" logical OR operator, "!" logical NOT operator

(I) "&&" when both operands are Boolean values When, the logical AND operation is performed on them, that is: if and only if both Boolean values ​​are true, the result is true, otherwise it is returned false.

Note: Actual performance

"&&" will detect the Boolean value of the first expression operand. If the first operand expression returns false, the first operand on the left will be returned. The value of the expression: false; otherwise, the second operand expression on the right will continue to be detected, and then the value of the second operand expression will be returned;

For example: if (a = b) stop( ); Equivalent to (a = b) && stop();

This method is deprecated because the code on the right side of the operator is not guaranteed to be executed,

For example: if (( a

It will be simpler and safer to regard "&&" as a Boolean algebra operator.

(II) "||" When both operands are Boolean values, logical OR performs an OR operation on them, that is: when one of the two Boolean values ​​is true, the result is true. , otherwise return false.

Note: Actual performance

"||" will detect the Boolean value of the first expression operand. If the first operand expression returns true, the first operation on the left will be returned. The value of the number expression: true; otherwise, the second operand expression on the right will continue to be detected, and then the value of the second operand expression will be returned;

This method is also deprecated because the operation The code on the right side of the | |" is considered as a Boolean algebra operator, which is simpler and safer.

(III) "!" logical negation is a unary operator, placed before the operand, and its purpose is to negate the operand.

6. Assignment operator

(I) "=" is the assignment operator; it always expects the operand on the left to be a variable or an element of an array Or an attribute of an object;Expect the right side to be an arbitrary value of any type;

Associativity from right to left, if there are multiple assignment operators in an expression, then Count from the far right.

Note: Each assignment expression has a value, which is the value on the right side of the operator;

(II) You can use the assignment operation with operation

" = " After adding the value on the left to the value on the right, it is assigned to the value on the left. "-=" "/=" "*=" has the same method;

7. Other operators

The "?:" conditional operator is the only three Meta operator;

The Boolean result of an expression? Expression 1 (any value of any type): Expression 2 (any value of any type);

According to the first operation The Boolean value result of the number. If it is true, the second operand expression is executed and the value of the second operand expression is returned; if the Boolean value result of the first operand is false, the third operation is executed. Numeric expression, returns the value of the third operand expression.

3. Notes on JavaScript operators

1. Pay attention to the data type passed to the operator and the data type returned! Different operators expect their operand expressions to be calculated The result conforms to a certain data type.

For example: String multiplication cannot be performed, "a" * "b" is illegal, but, when possible, javascript will convert the expression into the correct type, so , the expression "3" * "5" is legal. JavaScript converts the string into a number and performs the operation. The result is the number 15, not the string "15".

2. Different performances depending on the operands:

String string = string (connected); "a" "b" = "ab" "5" "6" = "11"

String number = (string converted to number) string (concatenated); "a" 5 = "a5" 5 is converted to string "1" 0 = "10"

Number Number = Number (Add) 5 5 = 10.

3. Pay attention to the associativity of operators. Some operators are associative from left to right; some are associative from right to left.

For example: w = a b c is equivalent to w = (a b) c;

w = ---b is equivalent to w = - ( - ( -b ) ); w = a = b = c is equivalent to w= ( a = ( b = c ))

The associativity of unary operators, assignment operators, and ternary operators is from right to left;

【 Recommended learning: javascript advanced tutorial

The above is the detailed content of How many JavaScript operators are there?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

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.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

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.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

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.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

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.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

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

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

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: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

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.

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

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.