search
HomeWeb Front-endJS TutorialTake you step by step to understand the basics of JavaScript operators

This article will take you through the basic knowledge of operators in JavaScript: arithmetic operators, increment/decrement operators, comparison operators, logical operators and ternary operators. I hope to be helpful.

Take you step by step to understand the basics of JavaScript operators

Operator

1 Arithmetic operator

  • Concept: It is to perform Operators for addition, subtraction, multiplication, and division, and remainder calculations

  • Operators: , -, *, /, % (remainder operations)

Note: When performing arithmetic operations, floating point (decimal) operations may cause precision problems

2 Increment and decrement operators

Operators: , -- (a, a--, a, --a)

//前置递增运算符
var num = 1;
++num //或者num++
console.log(num)//结果为2
 
//++num 相当于是 num = num + 1
//前置++ 是先做自增再做其他运算
 
 
 
 
//前置递减运算符
var num = 1;
--num //或者num--
console.log(num)//结果为0
 
//--num 相当于是 num = num - 1
//前置-- 是先做自减再做其他运算

3 Comparison operators

Operators: > , = ,

Note: The more Small first execution

  • ##> , = ,
  • == , !=, ===, !== The priority is 7

  • ==: Determine whether the values ​​​​in the data on both sides are equal (different data types will be converted to the same Data type conversion 18=='18' // true)

  • ===: Whether the values ​​in the two passes of data are of the same type (18==='18' // false)

4 Logical operator

##Logical AND (&&):

    If the && symbol is used, it means that only when both conditions are true (true), the result of the entire expression is true. As long as one condition is false, the expression The result of the formula is false
  • The interruption of logical AND:

    Because the AND operation requires all conditions to be established, the final AND operation result is true, if one of the conditions is not true, the result of the AND operation is false
  • var age = 18
    var num ;
    age>18 && (num = 998);
     
    //因为 age>18没有成立,逻辑与就已经得到结果为假
    //所以当逻辑与计算完毕之后,后面的num=998就不会再运行了
  • Logical OR (||):

    As long as there is one condition If it is true, the result of the expression is true. Only when all conditions are not true, the result of the expression is false.
  • Interruption of logical OR:
var age = 18;
var num;
age == 18 || (num = 998);
 
 //因为 age==18成立,逻辑或就已经得到结果为真
//所以当逻辑或计算完毕之后,后面的num=998就不会再运行了

Logical NOT (!): Negate true to false, false to true

var a = 5;
!(a > 1)//a等于5,所以大于1为真(true),因为取反,所以这个表达式为假(false)

5 Ternary operator: ?:

can be understood as a simplified way of writing the double branch of if

Grammar structure:

表达式1 ? 表达式2 : 表达式3

When expression 1 When it is established, expression 2

will be executed. When expression 1 is not established, expression 3

var a,b=2,c=3;
a=b>2?b:c; //运行结果是a为3,b大于2为真就返回b给a,为假返回c给a,因为b不大于2,所以返回c给a

will be executed. [Recommended learning:

javascript advanced tutorial

The above is the detailed content of Take you step by step to understand the basics of JavaScript operators. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

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

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.