JavaScript Operators Practice Questions with Solutions

Introductions

JavaScript operators are symbols and keywords used to perform calculations, comparisons, assignments, and logical operations. They are used in almost every JavaScript program. In this chapter, you will practice arithmetic, assignment, comparison, logical, increment, decrement, and ternary operators through simple examples. The questions gradually become more practical so that beginners can understand how operators work and when to use them. JavaScript Operators practice questions with solutions help to understand the concepts.

Question 1: Add Two Numbers

Problem

Create two variables containing 25 and 15. Use the addition operator to calculate and display their sum.

Solution

letnumber1=25;letnumber2=15;letresult=number1+number2;console.log(result);

Output

40

Step-by-step Explanation

  1. number1 stores 25.
  2. number2 stores 15.
  3. The + operator adds both numbers.
  4. 25 + 15 gives 40.
  5. The result is stored in result.
  6. console.log() displays the result.

Question 2: Perform Basic Arithmetic Operations

Problem

Create two numbers, 20 and 6. Calculate their addition, subtraction, multiplication, and division.

Solution

leta=20;letb=6;console.log(a+b);console.log(a-b);console.log(a*b);console.log(a/b);

Output

26141203.3333333333333335

Step-by-step Explanation

  1. + performs addition: 20 + 6 = 26.
  2. - performs subtraction: 20 - 6 = 14.
  3. * performs multiplication: 20 * 6 = 120.
  4. / performs division: 20 / 6 gives approximately 3.33.

These are called arithmetic operators.


Question 3: Find the Remainder Using the Modulus Operator

Problem

A student has 17 chocolates and wants to distribute them equally among 5 students. Use the modulus operator to find how many chocolates are left.

Solution

letchocolates=17;letstudents=5;letremaining=chocolates%students;console.log(remaining);

Output

2

Step-by-step Explanation

  1. chocolates contains 17.
  2. students contains 5.
  3. The % operator returns the remainder after division.
  4. 17 ÷ 5 leaves a remainder of 2.
  5. Therefore, remaining contains 2.

The % operator is called the modulus operator.


Question 4: Use Assignment Operators

Problem

Create a variable score with the value 100. Add 20 to it using the += operator and then subtract 10 using -=.

Solution

letscore=100;score+=20;score-=10;console.log(score);

Output

110

Step-by-step Explanation

  1. Initially, score is 100.
  2. score += 20 means score = score + 20.
  3. The new score becomes 120.
  4. score -= 10 means score = score - 10.
  5. The final score becomes 110.

Assignment operators provide a shorter way to update variable values.


Question 5: Compare Two Numbers

Problem

Create two variables containing 50 and 30. Check whether the first number is greater than the second number.

Solution

letnumber1=50;letnumber2=30;console.log(number1>number2);

Output

true

Step-by-step Explanation

  1. number1 contains 50.
  2. number2 contains 30.
  3. The > operator checks whether the left value is greater than the right value.
  4. 50 is greater than 30.
  5. Therefore, JavaScript returns true.

Comparison operators return a Boolean value: true or false.


Question 6: Compare Values Using Strict Equality

Problem

Create two variables containing the number 10 and the string "10". Check whether they are strictly equal.

Solution

letnumberValue=10;lettextValue="10";console.log(numberValue===textValue);

Output

false

Step-by-step Explanation

  1. numberValue contains the number 10.
  2. textValue contains the string "10".
  3. === checks both the value and the data type.
  4. One value is a number and the other is a string.
  5. Therefore, the result is false.

This is why === is generally preferred when you want a strict comparison.


Question 7: Use the Logical AND Operator

Problem

A student can enter a competition only if their age is at least 14 and they have a registration card. Write a JavaScript program to check both conditions.

Solution

letage=15;lethasRegistrationCard=true;letcanEnter=age>=14&&hasRegistrationCard;console.log(canEnter);

Output

true

Step-by-step Explanation

  1. age >= 14 checks whether the student is at least 14 years old.
  2. The result is true because the age is 15.
  3. hasRegistrationCard is also true.
  4. The && operator requires both conditions to be true.
  5. Both conditions are true, so canEnter becomes true.

The && operator is called the logical AND operator.


Question 8: Use the Logical OR Operator

Problem

A student can receive a reward if they score more than 90 marks or complete a special challenge. Check whether the student qualifies.

Solution

letmarks=85;letcompletedChallenge=true;letgetsReward=marks>90||completedChallenge;console.log(getsReward);

Output

true

Step-by-step Explanation

  1. marks > 90 checks whether the marks are greater than 90.
  2. The result is false because the student scored 85.
  3. completedChallenge is true.
  4. The || operator returns true if at least one condition is true.
  5. Therefore, getsReward is true.

The || operator is called the logical OR operator.


Question 9: Increase a Value Using the Increment Operator

Problem

A player has a score of 50. Increase the score by 1 using the increment operator.

Solution

letscore=50;score++;console.log(score);

Output

51

Step-by-step Explanation

  1. The variable score initially contains 50.
  2. ++ increases the value by 1.
  3. The score becomes 51.
  4. console.log(score) displays 51.

The following two statements produce the same final value:

score++;

and

score=score+1;

Question 10: Use the Ternary Operator

Problem

Create a variable named age. If the age is 18 or above, display "Adult". Otherwise, display "Minor" using the ternary operator.

Solution

letage=20;letresult=age>=18?"Adult":"Minor";console.log(result);

Output

Adult

Step-by-step Explanation

  1. age >= 18 checks the condition.
  2. The ? separates the condition from the first result.
  3. "Adult" is returned when the condition is true.
  4. The : separates the true and false results.
  5. "Minor" is returned when the condition is false.
  6. Since age is 20, the condition is true.
  7. Therefore, the output is Adult.

The ternary operator is useful for writing a simple condition in a single expression.

Key Takeaways

  • Operators are used to perform different operations in JavaScript.
  • + performs addition.
  • - performs subtraction.
  • * performs multiplication.
  • / performs division.
  • % returns the remainder after division.
  • = assigns a value to a variable.
  • += and -= update a variable using its existing value.
  • > and < compare values.
  • === performs a strict equality comparison.
  • && requires both conditions to be true.
  • || requires at least one condition to be true.
  • ++ increases a value by one.
  • The ternary operator provides a short way to handle a simple condition.

FAQs

1. What are operators in JavaScript?

Operators are symbols or keywords used to perform operations on values and variables, such as calculations, comparisons, assignments, and logical operations.

2. What is the difference between = and ===?

= is an assignment operator used to assign a value, while === is a comparison operator that checks both value and data type.

letage=18;console.log(age===18);

The result is true.

3. What does the % operator do in JavaScript?

The % operator returns the remainder after division.

console.log(17%5);

Output:

2

4. What does && mean in JavaScript?

&& is the logical AND operator. It requires the conditions involved to be true for the overall logical expression to evaluate to true.

5. What does || mean in JavaScript?

|| is the logical OR operator. It evaluates to true when at least one of the conditions is true.

6. What does ++ do in JavaScript?

The ++ operator increases a numeric value by one.

letcount=5;count++;console.log(count);

Output:

6

7. What is the ternary operator in JavaScript?

The ternary operator is a short way to write a simple conditional expression. It uses the ? and : symbols.

letage=20;letresult=age>=18?"Adult":"Minor";

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top