JavaScript PART 3: OPERATORS AND EXPRESSIONS IN JavaScript
Please Subscribe Youtube| Like Facebook | Follow Twitter
Operators and Expressions in JavaScript
In this article, we’ll cover the most commonly used operators and expressions in JavaScript, along with their syntax, examples, and outputs.
In JavaScript, operators and expressions are the building blocks of programming that help in manipulating data and making decisions based on conditions. Understanding these concepts is crucial for any developer who wants to write efficient and maintainable code in JavaScript. This article will cover the various types of operators and expressions in JavaScript, along with example code and outputs.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations on numeric data types. Here are some commonly used arithmetic operators in JavaScript:
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 |
– | Subtraction | 5 – 3 |
* | Multiplication | 5 * 3 |
/ | Division | 5 / 3 |
% | Modulus | 5 % 3 |
Let’s look at an example program that uses arithmetic operators in JavaScript:
let num1 = 5;
let num2 = 3;
let result = num1 + num2;
console.log(`Addition: ${result}`); // Output: Addition: 8
result = num1 - num2;
console.log(`Subtraction: ${result}`); // Output: Subtraction: 2
result = num1 * num2;
console.log(`Multiplication: ${result}`); // Output: Multiplication: 15
result = num1 / num2;
console.log(`Division: ${result}`); // Output: Division: 1.6666666666666667
result = num1 % num2;
console.log(`Modulus: ${result}`); // Output: Modulus: 2
Output
Addition: 8 Subtraction: 2 Multiplication: 15 Division: 1.6666666666666667 Modulus: 2
Comparison Operators
Comparison operators are used to compare values and return a boolean value (true or false) based on the result. Here are some commonly used comparison operators in JavaScript:
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 |
!= | Not equal to | 5 != 3 |
> | Greater than | 5 > 3 |
< | Less than | 5 < 3 |
>= | Greater than or equal to | 5 >= 3 |
<= | Less than or equal to | 5 <= 3 |
Let’s look at an example program that uses comparison operators in JavaScript:
let num1 = 5;
let num2 = 3;
console.log(`Is ${num1} equal to ${num2}? ${num1 == num2}`); // Output: Is 5 equal to 3? false
console.log(`Is ${num1} not equal to ${num2}? ${num1 != num2}`); // Output: Is 5 not equal to 3? true
console.log(`Is ${num1} greater than ${num2}? ${num1 > num2}`); // Output: Is 5 greater than 3? true
console.log(`Is ${num1} less than ${num2}? ${num1 < num2}`); // Output: Is 5 less than 3? false
console.log(`Is ${num1} greater than or equal to ${num2}? ${num1 >= num2}`); // Output: Is 5 greater than or equal to 3? true
console.log(`Is ${num1} less than or equal to ${num2}? ${num1 <= num2}`); // Output: Is 5 less than or equal to 3? false
Output
Is 5 equal to 3? false Is 5 not equal to 3? true Is 5 greater than 3? true Is 5 less than 3? false Is 5 greater than or equal to 3? true Is 5 less than or equal to 3? false
Logical Operators
Logical operators are used to combine or manipulate boolean values. There are three logical operators in JavaScript:
Operator | Description | Example |
---|---|---|
&& | AND | true && false |
|| | OR | true || false |
! | NOT | !true |
Let’s look at an example program that uses logical operators in JavaScript:
let num1 = 5;
let num2 = 3;
let num3 = 7;
console.log(num1 > num2 && num1 < num3); // Output: true
console.log(num1 > num2 || num1 > num3); // Output: true
console.log(!(num1 > num2)); // Output: false
Output
true
true
false
Conclusion
In conclusion, JavaScript comparison operators are essential tools that allow us to compare variables and values. With these operators, we can perform simple or complex checks, make logical decisions, and execute conditional statements. It’s important to understand the behavior of each operator to avoid unexpected results or errors. Additionally, we must keep in mind that JavaScript has a set of rules for type coercion that can affect the outcome of comparisons. By using these operators correctly, we can create efficient and reliable code that meets the requirements of our projects.
JavaScript Beginner Tutorial Series
- JavaScript Part 1: Setup and Introduction
- JAVASCRIPT PART 2: Understanding Basic Data Types And Variables In JavaScript
- JavaScript PART 3: OPERATORS AND EXPRESSIONS IN JavaScript
- JavaScript PART 4: CONTROL FLOW STATEMENTS IN JavaScript
- JavaScript PART 5: FUNCTIONS IN JavaScript
- JavaScript Part 6: Arrays In JavaScript
- JavaScript Part 7: String Manipulation In JavaScript
- JavaScript Part 8: Object-Oriented Programming In JavaScript (Classes and Objects)
- JavaScript Part 9: Object-oriented Programming In JavaScript (OOP Pillars)
- JavaScript Part 10: Exception Handling in JavaScript