DEV Community

Cover image for JavaScript Basics: Operators and Expressions
Sharique Siddiqui
Sharique Siddiqui

Posted on

JavaScript Basics: Operators and Expressions

Once you’ve learned about variables and data types in JavaScript, the next big step is understanding operators and expressions. Operators are the building blocks that allow you to perform calculations, compare values, and make decisions in your code. In this blog, we’ll cover three important categories: arithmetic operators, comparison operators, and logical operators.

What are Operators and Expressions?

  • Operators: Symbols that tell JavaScript to perform specific actions (like addition, comparison, or logical checks).
  • Expressions: Combinations of values, variables, and operators that result in a single value.
Example:
javascript
let result = 5 + 3; // Expression: 5 + 3
console.log(result); // Output: 8
Enter fullscreen mode Exit fullscreen mode

Here, the + is an operator, and 5 + 3 is an expression.

1. Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 2 3
* Multiplication 4 * 2 8
/ Division 10 / 2 5
% Modulus (remainder) 10 % 3 1
++ Increment (by 1) let x=5; x++
-- Decrement (by 1) let y=5; y--
** Exponentiation 2 ** 3 8
Example:
javascript
let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a % b); // 1
console.log(a ** b); // 1000
Enter fullscreen mode Exit fullscreen mode

2. Comparison Operators

Comparison operators are used to compare two values. They return a Boolean (true or false).

Operator Description Example Result
== Equal to (compares values) 5 == "5" true
=== Strict equal (value & type) 5 === "5" false
!= Not equal (compares value) 5 != "6" true
!== Strict not equal (value/type) 5 !== "5" true
> Greater than 7 > 5 true
< Less than 3 < 8 true
>= Greater than or equal to 5 >= 5 true
<= Less than or equal to 4 <= 6 true
Example:
javascript
console.log(5 == "5");  // true (loose equality)
console.log(5 === "5"); // false (strict equality)
console.log(7 > 3);     // true
Enter fullscreen mode Exit fullscreen mode

3. Logical Operators

Logical operators are used to combine or invert conditions, often inside if statements or complex expressions.

Operator Name Description
&& AND Both conditions must be true
`\ \ `
! NOT Inverts a condition
Example:

(5 < 3 || 2 < 4) // true

Example:
javascript
let age = 20;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("You are allowed to enter.");
} else {
  console.log("Access denied.");
}
Enter fullscreen mode Exit fullscreen mode

&& ensures both conditions (age >= 18 and hasID) must be true.

If age was less than 18 or hasID was false, the condition would fail.

Final Thoughts

Understanding operators and expressions is crucial because they form the decision-making and calculation backbone of JavaScript programs.

  • Use arithmetic operators for math-related tasks.
  • Use comparison operators to compare values.
  • Use logical operators to combine conditions and control program flow.

Stay tuned for more insights as you continue your journey into the world of web development!

Check out theYouTubePlaylist for great JavaScript content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)