Operators in JavaScript

Operators in JavaScript

In JavaScript, an operator is a symbol that performs an operation on one or more operands (values or variables). For example, the addition operator + adds two numbers.

let a = 5;
let b = 10;
let c = a + b; // c is now 15

There are many different types of operators in JavaScript, including arithmetic, assignment, comparison, and logical operators.

Arithmetic Operators

Arithmetic operators perform mathematical operations on numbers. Here are the most commonly used arithmetic operators:

OperatorNameExampleResult
+Addition5 + 1015
-Subtraction5 - 10-5
*Multiplication5 * 1050
/Division10 / 52
%Modulus10 % 31
**Exponentiation2 ** 38
Commonly used arithmetic operators in JavaScript

Here is an example of using arithmetic operators:

let a = 10;
let b = 5;

console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
console.log(a ** b); // 100000

Assignment Operators

Assignment operators assign a value to a variable. The most basic assignment operator is the = operator, which assigns the value on the right to the variable on the left:

let a = 5; // a is now 5

There are also compound assignment operators that perform an operation and assign the result to the variable in a single step. For example, the += operator adds the value on the right to the variable on the left and assigns the result to the variable:

let a = 5;
a += 10; // a is now 15

Here is a list of all the compound assignment operators:

OperatorExampleSame As
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
**=a **= ba = a ** b
Compound assignment operators in JavaScript

Comparison Operators

Comparison operators compare two values and return a boolean value (true or false) based on the comparison. Here are the most commonly used comparison operators:

OperatorNameExampleResult
==Equal5 == 5true
!=Not equal5 != 5false
>Greater than5 > 5false
<Less than5 < 5false
>=Greater than or equal to5 >= 5true
<=Less than or equal to5 <= 5true
Commonly used comparison operators in JavaScript

Here is an example of using comparison operators:

console.log(5 == 5); // true
console.log(5 != 5); // false
console.log(5 > 5); // false
console.log(5 &lt; 5); // false
console.log(5 >= 5); // true
console.log(5 &lt;= 5); // true

Logical Operators

Logical operators perform logical operations on boolean values. The most commonly used logical operators are && (and), || (or), and ! (not).

The && operator returns true if both operands are true, and false otherwise:

console.log(true &amp;&amp; true); // true
console.log(true &amp;&amp; false); // false
console.log(false &amp;&amp; true); // false
console.log(false &amp;&amp; false); // false

The || operator returns true if either operand is true, and false otherwise:

console.log(true || true); // true
console.log(true || false); // true
console.log(false || true); // true
console.log(false || false); // false

The ! operator negates a boolean value:

console.log(!true); // false
console.log(!false); // true

Here is an example of using logical operators in a conditional statement:

let a = 5;
let b = 10;

if (a > 0 &amp;&amp; b > 0) {
  console.log("Both a and b are positive");
} else {
  console.log("Either a or b is not positive");
}

Practice Exercise

Here are some practice questions based on real-world scenarios that could be used to test understanding of the concepts covered in the tutorial on JavaScript operators:

  1. You are building a simple calculator that adds two numbers together. You need to use the + operator to add the numbers. What is the result of the following expression: 5 + 10?
  2. You are building a shopping cart that allows users to add items to their cart. Each time an item is added, the quantity of that item in the cart is incremented by 1. You need to use the ++ operator to increment the quantity. How would you increment the quantity of an item in a cart stored in a variable quantity?
  3. You are building a login system that checks if a user’s entered password is correct. You need to use the == operator to compare the user’s entered password to the correct password. Is the following expression true or false: "password123" == "password123"?
  4. You are building a to-do list that allows users to mark tasks as complete. You need to use the && operator to check if both tasks are marked as complete and the due date has passed. Is the following expression true or false: true && false?
  5. You are building a form that allows users to enter their age. You need to use the > operator to check if the user’s age is greater than 18. Is the following expression true or false: 25 > 18?

I hope this tutorial has helped you understand the different types of operators in JavaScript. Operators are an essential part of programming, and knowing how to use them is crucial for writing efficient and effective code.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.