JavaScript Part 3: Operators and Expressions in JavaScript

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:

OperatorDescriptionExample
+Addition5 + 3
Subtraction5 – 3
*Multiplication5 * 3
/Division5 / 3
%Modulus5 % 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:

OperatorDescriptionExample
==Equal to5 == 5
!=Not equal to5 != 3
>Greater than5 > 3
<Less than5 < 3
>=Greater than or equal to5 >= 3
<=Less than or equal to5 <= 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:

OperatorDescriptionExample
&&ANDtrue && false
||ORtrue || 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

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *