Home >Web Front-end >JS Tutorial >Demystifying JavaScript Operators: What Does That Symbol Mean?
This article provides a comprehensive guide to JavaScript operators, categorized for clarity and enhanced understanding. We'll explore their functions and illustrate their usage with practical examples.
Key Concepts:
1. Arithmetic Operators: These perform standard mathematical operations.
'Hello' ' World!'
results in 'Hello World!'
. Note the behavior with objects: 1 {a:1}
yields '1[object Object]'
.10 - 5
equals 5
.equals
10`.10 / 2
equals 5
. Division by zero results in Infinity
. BigInt division truncates the result.10 % 3
equals 1
. x
increments before use, postfix x
increments after).--x
, postfix x--
).-5
negates 5
. '10'
becomes 10
.equals
8`.2. Assignment Operators: Assign values to variables, often combining operations with assignment.
x = 5;
x = 3;
(equivalent to x = x 3;
)x -= 2;
x /= 2;
x %= 3;
&=
, |=
, ^=
, <<=
, >>=
, >>>=
).3. Comparison Operators: Compare values, returning a Boolean result.
1 == '1'
is true
.1 != '2'
is true
.1 === '1'
is false
.1 !== '1'
is true
.5 > 2
is true
.2 < 5
is true
.5 >= 5
is true
.2 <= 5
is true
.4. Logical Operators: Combine or modify Boolean expressions.
true && false
is false
. 'a' && 'b'
is 'b'
.false || true
is true
. '' || 'a'
is 'a'
.!true
is false
.null
or undefined
. null ?? 'default'
is 'default'
. 0 ?? 'default'
is 0
.5. Bitwise Operators: Operate on the binary representations of numbers.
5 & 3
(binary 101 & 011
) equals 1
(binary 001
).5 | 3
(binary 101 | 011
) equals 7
(binary 111
).5 ^ 3
(binary 101 ^ 011
) equals 6
(binary 110
).~5
(binary ~101
) equals -6
.6. Other Operators:
age > 18 ? 'Adult' : 'Minor';
[...array]
creates a copy.obj?.prop?.subprop
Operator Precedence: JavaScript follows specific rules for the order of operations. Parentheses ()
can override precedence.
This detailed explanation provides a solid foundation for understanding and effectively utilizing JavaScript operators in your programming endeavors. Remember to consult the MDN Web Docs for the most up-to-date and comprehensive information.
The above is the detailed content of Demystifying JavaScript Operators: What Does That Symbol Mean?. For more information, please follow other related articles on the PHP Chinese website!