0
0
Javascriptprogramming~5 mins

Why operators are needed in Javascript

Choose your learning style9 modes available
Introduction

Operators help us do math and make decisions in code easily. They let us add, compare, and change values quickly.

When you want to add two numbers like prices in a shopping cart.
When you need to check if a user is old enough to sign up.
When you want to combine text like first and last names.
When you want to repeat actions while a condition is true.
When you want to change a value based on some rule.
Syntax
Javascript
// Example of operators
let sum = 5 + 3; // + is an operator
let isAdult = age >= 18; // >= is an operator
let message = 'Hi, ' + name; // + joins text

Operators work on values to produce new values.

Different operators do different jobs like math, comparison, or logic.

Examples
Adds two numbers to get 30.
Javascript
let total = 10 + 20;
Checks if two values are exactly the same, returns true.
Javascript
let isEqual = (5 === 5);
Joins two pieces of text into one.
Javascript
let greeting = 'Hello, ' + 'friend!';
Sample Program

This program adds price and tax using + operator and checks if age is 18 or more using >= operator.

Javascript
let price = 50;
let tax = 5;
let totalPrice = price + tax;

let age = 20;
let canVote = age >= 18;

console.log('Total price:', totalPrice);
console.log('Can vote:', canVote);
OutputSuccess
Important Notes

Remember, operators make code shorter and easier to read.

Using the wrong operator can cause bugs, so choose carefully.

Summary

Operators let you do math and comparisons in code.

They help combine values and make decisions.

Using operators correctly makes your code work well and be easy to understand.