Home Web Front-end Front-end Q&A Different types of operations in JavaScript

Different types of operations in JavaScript

May 09, 2023 pm 08:36 PM

JavaScript is a widely used programming language used for front-end and back-end development. It contains many different types of operators, which have their unique applications in different situations. This article will introduce in detail the different types of operators in JavaScript and how to use them.

1. Arithmetic operators

Arithmetic operators are the most basic operators for processing numeric type data. They can be used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, etc.

1. Addition operator ( )

The addition operator is used to add two numbers. Additionally, it can also concatenate strings together. For example:

var x = 10;
var y = 5;
var z = x + y;
//输出结果为15
Copy after login

2. Subtraction operator (-)

The subtraction operator is used to subtract two numbers. For example:

var x = 10;
var y = 5;
var z = x - y;
//输出结果为5
Copy after login

3. Multiplication operator (*)

The multiplication operator is used to multiply two numbers. For example:

var x = 10;
var y = 5;
var z = x * y;
//输出结果为50
Copy after login

4. Division operator (/)

The division operator is used to divide one number by another number. For example:

var x = 10;
var y = 5;
var z = x / y;
//输出结果为2
Copy after login

5. Remainder operator (%)

The remainder operator is used to calculate the remainder after dividing two numbers. For example:

var x = 10;
var y = 4;
var z = x % y;
//输出结果为2
Copy after login

2. Comparison operators

Comparison operators are used to compare two values ​​and return a Boolean value (true or false).

1. Equal operator (==)

The equal operator is used to compare whether two values ​​are equal. For example:

var x = 10;
var y = "10";
if (x == y) {
    //执行代码
}
Copy after login

At this time, although the data types of x and y are different, because their values ​​are equal, the condition is judged to be true.

2. Not equal to operator (!=)

The not equal to operator is used to compare whether two values ​​are not equal. For example:

var x = 10;
var y = "5";
if (x != y) {
    //执行代码
}
Copy after login

At this time, since the values ​​​​of x and y are not equal, the condition is judged to be true.

3. Strict equal operator (===)

The strict equal operator is used to compare whether two values ​​are not only equal, but also have the same data type. For example:

var x = 10;
var y = "10";
if (x === y) {
    //执行代码
}
Copy after login

At this time, because the data types of x and y are different, the condition is judged to be false.

4. Strict inequality operator (!==)

The strict inequality operator is used to compare whether two values ​​are not only not equal, but also have different data types. For example:

var x = 10;
var y = "5";
if (x !== y) {
    //执行代码
}
Copy after login

Since the data types of x and y are different, and their values ​​are not equal, the condition is judged to be true.

5. Greater than operator (>), less than operator (<), greater than or equal to operator (>=) and less than or equal to operator (<=)

these The usage of operators is the same as comparison operators. For example:

var x = 10;
var y = 5;
if (x > y) {
    //执行代码
}
Copy after login

This condition is judged to be true because 10 is greater than 5.

3. Logical operators

Logical operators are used to perform logical operations on two or more expressions and return a Boolean value (true or false).

1. Logical AND operator (&&)

The logical AND operator is used to perform logical AND (AND) operations on two expressions. For example:

var x = 10;
var y = 5;
if (x > 5 && y < 10) {
    //执行代码
}
Copy after login

Since x is greater than 5 and y is less than 10, the condition is judged to be true.

2. Logical OR operator (||)

The logical OR operator is used to logically OR (or) operate two expressions. For example:

var x = 10;
var y = 5;
if (x > 5 || y > 10) {
    //执行代码
}
Copy after login

Because x is greater than 5 or y is greater than 10, the condition is judged to be true.

3. Logical NOT operator (!)

The logical NOT operator is used to negate an expression. For example:

var x = 10;
var y = 5;
if (!(x > y)) {
    //执行代码
}
Copy after login

At this time, the condition that x is greater than y is judged to be true, but due to the addition of the inverse logical NOT operator, the final condition is judged to be false.

4. Bit operators

Bit operators perform operations on the binary representation of values. They operate on each bit in the binary representation of the number separately.

1. Bitwise AND operator (&)

The bitwise AND operator performs an AND operation on the binary values ​​of two numbers. For example:

var x = 5 & 1;
//输出结果为1
Copy after login

2. Bitwise OR operator (|)

The bitwise OR operator performs an OR operation on the binary values ​​of two numbers. For example:

var x = 5 | 1;
//输出结果为5
Copy after login

3. Bitwise XOR operator (^)

The bitwise XOR operator performs an XOR operation on the binary values ​​of two numbers. For example:

var x = 5 ^ 1;
//输出结果为4
Copy after login

4. Negation operator (~)

The negation operator performs the inversion operation on the binary value of a number. For example:

var x = ~5;
//输出结果为-6
Copy after login

5. Left shift operator (<<)

The left shift operator moves the binary representation of a number to the left by the specified number of digits. For example:

var x = 5 << 2;
//输出结果为20
Copy after login

6. Right shift operator (>>)

The right shift operator moves the binary representation of a number to the right by the specified number of digits. For example:

var x = 5 >> 2;
//输出结果为1
Copy after login

The above are the different types of operators in JavaScript and how to use them. Understanding the application of these operators allows developers to write better code and improve code execution efficiency.

The above is the detailed content of Different types of operations in JavaScript. 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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

What is useEffect? How do you use it to perform side effects? What is useEffect? How do you use it to perform side effects? Mar 19, 2025 pm 03:58 PM

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

How does the React reconciliation algorithm work? How does the React reconciliation algorithm work? Mar 18, 2025 pm 01:58 PM

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code? What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code? Mar 18, 2025 pm 01:44 PM

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

How does currying work in JavaScript, and what are its benefits? How does currying work in JavaScript, and what are its benefits? Mar 18, 2025 pm 01:45 PM

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

What is useContext? How do you use it to share state between components? What is useContext? How do you use it to share state between components? Mar 19, 2025 pm 03:59 PM

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

How do you connect React components to the Redux store using connect()? How do you connect React components to the Redux store using connect()? Mar 21, 2025 pm 06:23 PM

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

How do you prevent default behavior in event handlers? How do you prevent default behavior in event handlers? Mar 19, 2025 pm 04:10 PM

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

What are the advantages and disadvantages of controlled and uncontrolled components? What are the advantages and disadvantages of controlled and uncontrolled components? Mar 19, 2025 pm 04:16 PM

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.

See all articles