Basics
JS Operators
JavaScript Operators Overview
JavaScript operators include arithmetic and logical, with precedence rules.
Introduction to JavaScript Operators
JavaScript operators allow you to perform various operations on variables and values. They are classified into different categories based on their functionality, such as arithmetic, logical, comparison, assignment, and more. Understanding how these operators work is crucial for writing effective JavaScript code.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. Here are some basic arithmetic operators:
- +: Addition
- -: Subtraction
- *: Multiplication
- /: Division
- %: Modulus (remainder)
Logical Operators
Logical operators are used to combine two or more conditions. They typically return a boolean value based on the logic of the conditions. Here are the main logical operators:
- &&: Logical AND
- ||: Logical OR
- !: Logical NOT
These operators are often used in control structures like if
statements.
Operator Precedence
Operator precedence determines the order in which operations are performed when an expression has multiple operators. Operators with higher precedence are evaluated before operators with lower precedence. For example, multiplication and division have higher precedence than addition and subtraction.
Consider the following expression:
In the above example, multiplication is performed before addition, resulting in 20
instead of 30
.
To override the default precedence, you can use parentheses:
Conclusion
Understanding JavaScript operators and their precedence rules is essential for writing clear and efficient code. Operators allow you to manipulate data, control program flow, and create complex expressions. In the next post, we'll explore the ternary operator, which offers a concise way to perform conditional operations.
Basics
- Introduction
- Where To
- Output
- Syntax
- Comments
- Variables
- Scope
- Hoisting
- Errors
- Data Types
- Operators
- Ternary Operator
- Short-Circuit Evaluation
- If Else
- Switch
- Loops
- For...Of/For...In
- Functions
- this Keyword
- Objects
- Arrays
- Strings
- Template Literals
- Numbers
- Number Properties
- Dates
- Math
- Booleans
- Type Conversion
- Destructuring
- Spread/Rest
- RegExp
- Strict Mode
- Modules
- Security Basics
- Debugging
- Best Practices
- Mistakes
- Performance
- Reserved Words
- Sets
- Maps
- Bitwise
- Array Const
- Previous
- Data Types
- Next
- Ternary Operator