Skip to content

JavaScript Operators

Introduction

In JavaScript, understanding comparisons and operators is important for effective scripting. This guide will delve into comparison operators, arithmetic operations, and string concatenation, ensuring a comprehensive grasp of these essential concepts.

JavaScript Comparison Operators

Equality and Inequality

Strict (===) and Loose (==) Equality

Strict equality (===) checks both value and type, while loose equality (==) converts types before comparison.


Output appears here after Run.

Greater Than (>) and Less Than (<)

These operators are used for comparing numeric values.


Output appears here after Run.

Special Cases in Comparisons

Comparing Non-Numeric Values

JavaScript performs type coercion, often leading to unexpected outcomes.


Output appears here after Run.

NaN Comparisons

NaN (Not-A-Number) is not equal to any value, including itself.


Output appears here after Run.

Arithmetic Operators in JavaScript

Basic Operators: Addition (+), Subtraction (-), Multiplication (*), and Division (/)

These operators are used for basic arithmetic operations.


Output appears here after Run.

Modulus (%) and Exponentiation (**)

Modulus returns the division remainder, while exponentiation raises a number to the power of another.


Output appears here after Run.

String Concatenation and the Binary + Operator

Concatenating Strings

In JavaScript, the + operator is used for both numeric addition and string concatenation.


Output appears here after Run.

Binary + and Type Coercion

When one operand is a string, JavaScript converts the other to a string as well.


Output appears here after Run.

Best Practices for Concatenation

TIP

Use template literals for clarity and avoid confusion with numeric addition.


Output appears here after Run.

Conclusion

Understanding comparisons, arithmetic operators, and string concatenation is fundamental in JavaScript. This knowledge forms the basis of numerous programming tasks, from simple calculations to complex decision-making processes.

Practice

Which of the following statements about JavaScript comparisons are correct?

Dual-run preview — compare with live Symfony routes.