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
number1stores25.number2stores15.- The
+operator adds both numbers. 25 + 15gives40.- The result is stored in
result. 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
+performs addition:20 + 6 = 26.-performs subtraction:20 - 6 = 14.*performs multiplication:20 * 6 = 120./performs division:20 / 6gives approximately3.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
chocolatescontains17.studentscontains5.- The
%operator returns the remainder after division. 17 ÷ 5leaves a remainder of2.- Therefore,
remainingcontains2.
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
- Initially,
scoreis100. score += 20meansscore = score + 20.- The new score becomes
120. score -= 10meansscore = score - 10.- 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
number1contains50.number2contains30.- The
>operator checks whether the left value is greater than the right value. 50is greater than30.- 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
numberValuecontains the number10.textValuecontains the string"10".===checks both the value and the data type.- One value is a number and the other is a string.
- 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
age >= 14checks whether the student is at least 14 years old.- The result is
truebecause the age is15. hasRegistrationCardis alsotrue.- The
&&operator requires both conditions to be true. - Both conditions are true, so
canEnterbecomestrue.
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
marks > 90checks whether the marks are greater than90.- The result is
falsebecause the student scored85. completedChallengeistrue.- The
||operator returnstrueif at least one condition is true. - Therefore,
getsRewardistrue.
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
- The variable
scoreinitially contains50. ++increases the value by1.- The score becomes
51. console.log(score)displays51.
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
age >= 18checks the condition.- The
?separates the condition from the first result. "Adult"is returned when the condition istrue.- The
:separates the true and false results. "Minor"is returned when the condition isfalse.- Since
ageis20, the condition is true. - 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.
