Home Web Front-end JS Tutorial JavaScript Enhancement Tutorial - JavaScript Operators

JavaScript Enhancement Tutorial - JavaScript Operators

Jan 21, 2017 pm 03:54 PM

This article is the official HTML5 training tutorial of the H5EDU organization. It mainly introduces: JavaScript intensive tutorial - JavaScript operators
JavaScript arithmetic operators

Arithmetic operators are used to perform operations on two variables or values.

Assign y = 5, the following table will explain to you the use of arithmetic operators:

Operator description example y value x value online example
+ addition x = y + 2 y = 5 x = 7 Example
- Subtraction x = y - 2 y = 5 x = 3 Example
* Multiplication x = y * 2 y = 5 x = 10 Example
/ Division x = y / 2 y = 5 x = 2.5 Example
% Remainder x = y % 2 y = 5 x = 1 Example
++ Increment x = ++y y = 6 x = 6 Example
x = y++ y = 6 x = 5 Example
-- Decrement x = --y y = 4 x = 4 Example
x = y-- y = 4 x = 5 Example

Regarding arithmetic operators, you You can read our JavaScript operators tutorial.

JavaScript assignment operator

The assignment operator is used to assign values ​​to JavaScript variables.

Given x=10 and y=5, the following table explains the assignment operator:

Operator Example Same As x Value Online Example
= x = y x = y x = 5 Examples
+= x += y x = x + y x = 15 Examples
-= x -= y x = x - y x = 5 Examples
*= x *= y x = x * y x = 50 Examples
/= x /= y x = x / y x = 2 Example
%= x %= y x = x % y x = 0 Example

For assignment operators, you can read our JavaScript operators Talisman tutorial.

JavaScript String Operators

+ operator, += operator can be used to concatenate strings.

Given text1 = "Good ", text2 = "Morning", and text3 = "", the following table explains the use of string operators:

Operator examples text1 text2 text3 Online example
+ text3 = text1 + text2 "Good " "Morning" "Good Morning" Example
+= text1 += text2 "Good Morning" "Morning" "" Example
Comparison operator

Comparison operators are used to judge logical statements to determine whether two given values ​​or variables are equal.

Given x=5, the following table shows the use of comparison operators:

Operator Description Comparison Result Online Example
== Equal to x == 8 false Example
x == 5 true instance
=== Values ​​and types are equal (constantly equal) x === "5" false instance
x === 5 true instance
!= is not equal to x != 8 true instance
!== The value and type are not equal (not equal) x !== "5" true instance
x !== 5 false instance
> greater than x > 8 false Example
< less than x < 8 true Example
>= greater than or equal to x >= 8 false Example
<= less than or equal to x <= 8 true Example

For comparison operators, you can read our JavaScript comparison operators tutorial.

Conditional operator

The conditional operator is used for assignment operations based on conditions.

Given x=6 and y=3, the following table demonstrates the operation of the conditional operator:

Syntax example online example
Variable = (condition) ? value 1: value 2 voteable = (age & 18) ? "Too young" : "Old enough" Example
Logical operators

Logical operators are used to determine the logical relationship between variables or values.

Given x=6 and y=3, the following example demonstrates the use of logical operators:

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x == 5 || y == 5) is false
! Not! (x == y) is true
JavaScript Bit Operator

Bit operators work on 32-bit numbers. Any numeric operations will be converted to 32 bits. The result is converted to a JavaScript number.
Operator description example is similar to the result decimal
& AND x = 5 & 1 0101 & 0001 0001 1
| OR x = 5 | 1 0101 | 0001 0101 5
~ Negate x = ~ 5 ~0101 1010 10
^ XOR x = 5 ^ 1 0101 ^ 0001 0100 4
<< Shift left/td> ##>> Right shift Chinese website (www.php.cn)!

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 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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Analysis of the meaning and usage of += operator in C language Analysis of the meaning and usage of += operator in C language Apr 03, 2024 pm 02:27 PM

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Python Operators: The Ultimate Guide from Newbie to Master Python Operators: The Ultimate Guide from Newbie to Master Mar 11, 2024 am 09:13 AM

Introduction to python operators Operators are special symbols or keywords used to perform operations between two or more operands. Python provides a variety of operators covering a wide range of uses, from basic mathematical operations to complex data manipulation. Mathematical operators Mathematical operators are used to perform common mathematical operations. They include: operator operation examples + addition a + b - subtraction a-b * multiplication a * b / division a / b % modulo operation (take the remainder) a % b ** power operation a ** b // integer division (discard the remainder) a//b Logical Operators Logical operators are used to concatenate Boolean values ​​and evaluate conditions. They include: operator operations examples and logical and aandbor logical or aorbnot logical not nota comparison operations

See all articles