JavaScript Operators & Type Conversion
Operators values par calculation, comparison aur logic perform karte hain. Type conversion ek value ko doosre type me convert karti hai. Dono concepts conditions, forms aur real application logic ke base hain.
What is an operator?
An operator is a symbol or keyword that performs an operation on one or more values. The values an operator works with are called operands.
10 + 5
score >= 40
isLoggedIn && hasAccess10 + 5 me + operator hai aur 10 aur 5 operands hain. Operator batata hai ki values ke saath kya operation karna hai.
Arithmetic operators
Arithmetic operators numeric calculations ke liye use hote hain.
console.log(10 + 3); // 13
console.log(10 - 3); // 7
console.log(10 * 3); // 30
console.log(10 / 2); // 5
console.log(10 % 3); // 1
console.log(2 ** 3); // 8+addition.-subtraction.*multiplication./division.%remainder.**exponentiation.
Remainder operator
The remainder operator is useful for patterns such as checking whether an integer is even.
const value = 12;
console.log(value % 2); // 0If an integer divided by 2 leaves remainder 0, it is even.
Increment and decrement
++ increases a numeric variable by one and -- decreases it by one.
let count = 5;
count++;
console.log(count); // 6
count--;
console.log(count); // 5Prefix and postfix forms can produce different expression results. Beginners should avoid clever combinations and keep updates on clear separate lines.
Assignment operators
The basic assignment operator is =. Compound assignment combines an operation with assignment.
let score = 50;
score += 10; // score = score + 10
score -= 5;
score *= 2;
score /= 5;
score %= 6;
console.log(score);= assigns a value. Equality operators compare values.The + operator with strings
The + operator can add numbers, but if string concatenation is involved it can join text instead.
console.log(10 + 5); // 15
console.log("10" + 5); // "105"
console.log("Broun" + "Stack"); // "BrounStack"+ ke saath string aane par result surprising ho sakta hai. Form input values often strings hote hain, isliye numeric calculation se pehle conversion samajhna bahut important hai.
Comparison operators
Comparison operators usually produce a boolean value: true or false.
console.log(10 > 5); // true
console.log(10 < 5); // false
console.log(10 >= 10); // true
console.log(8 <= 7); // falseStrict equality and inequality
=== checks equality without performing type coercion. !== checks strict inequality.
console.log(5 === 5); // true
console.log(5 === "5"); // false
console.log(5 !== "5"); // true=== and !== unless you intentionally need loose-equality coercion behavior.Loose equality
== and != can convert operand types before comparison. This can create results that are less obvious to beginners.
console.log(5 == "5"); // true
console.log(5 === "5"); // false
console.log(false == 0); // true
console.log(false === 0); // falseLoose equality has defined rules, but strict equality usually makes intent easier to understand.
Logical operators
Logical operators combine or invert conditions.
const isLoggedIn = true;
const hasCourse = true;
console.log(isLoggedIn && hasCourse); // true
console.log(isLoggedIn || hasCourse); // true
console.log(!isLoggedIn); // false&&— logical AND.||— logical OR.!— logical NOT.
Short-circuit behavior
&& and || do not always return booleans. They evaluate operands and can return one of the original values.
console.log("ready" && "start"); // "start"
console.log("" || "fallback"); // "fallback"This behavior is useful in real code, but understand truthy/falsy conversion before using it heavily.
Nullish coalescing operator
?? returns the right-hand value only when the left-hand value is null or undefined.
const savedName = null;
const displayName = savedName ?? "Guest";
console.log(displayName); // "Guest"Unlike ||, values such as 0 and an empty string are not treated as missing by ??.
Operator precedence
Some operators are evaluated before others. Multiplication normally happens before addition.
console.log(2 + 3 * 4); // 14
console.log((2 + 3) * 4); // 20What is type conversion?
Type conversion changes a value from one data type to another. Conversion can be explicit, where you ask for it, or implicit, where JavaScript performs coercion during an operation.
Convert to Number
Number() attempts to convert a value to the number type.
console.log(Number("42")); // 42
console.log(Number("3.5")); // 3.5
console.log(Number("")); // 0
console.log(Number(null)); // 0
console.log(Number(undefined)); // NaN
console.log(Number("hello")); // NaNAfter uncertain numeric conversion, Number.isNaN() can help detect an actual NaN result.
const amount = Number("hello");
console.log(Number.isNaN(amount)); // trueparseInt() and parseFloat()
parseInt() and parseFloat() parse numeric text from the beginning of a string. Their behavior differs from Number().
console.log(parseInt("42px", 10)); // 42
console.log(parseFloat("3.14rem")); // 3.14
console.log(Number("42px")); // NaNUse the conversion method that matches your data instead of treating them as interchangeable.
Convert to String
String() converts many values to text.
console.log(String(42)); // "42"
console.log(String(true)); // "true"
console.log(String(null)); // "null"
console.log(String(undefined)); // "undefined"Convert to Boolean
Boolean() converts a value according to JavaScript truthiness rules.
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("hello")); // true
console.log(Boolean("")); // false
console.log(Boolean(null)); // falseTruthy and falsy values
In boolean contexts, some values behave like false; most other values behave like true.
Common falsy values include false, 0, -0, 0n, an empty string, null, undefined and NaN.
console.log(Boolean("false")); // true
console.log(Boolean("0")); // true
console.log(Boolean([])); // true
console.log(Boolean({})); // trueString ke andar text kya likha hai usse boolean conversion automatically meaning nahi samajhta. Non-empty string generally truthy hoti hai, isliye "false" bhi truthy hai.
Implicit type coercion
JavaScript sometimes converts values automatically while evaluating an expression.
console.log("5" + 2); // "52"
console.log("5" - 2); // 3
console.log("5" * 2); // 10
console.log(true + 1); // 2The + operator is especially important because it also performs string concatenation.
Why this matters in forms
Many form controls expose entered values as strings. Numeric-looking input may therefore need explicit conversion before arithmetic.
const firstInput = "10";
const secondInput = "5";
console.log(firstInput + secondInput); // "105"
console.log(Number(firstInput) + Number(secondInput)); // 15We will apply this pattern to real forms later in the course.
Number and BigInt do not mix directly
Regular number and bigint values generally cannot be mixed directly in arithmetic.
const big = 10n;
const normal = 5;
// big + normal; // TypeErrorConvert deliberately only when that conversion is valid for your use case.
Beginner best practices
- Comparison ke liye default
===aur!==rakho. - User/form values par numeric arithmetic se pehle type verify ya convert karo.
- Conversion explicit rakho when intent matters.
+ke string-concatenation behavior ko remember karo.- Complex expressions me parentheses use karo.
Number.isNaN()se failed numeric conversion inspect karo.- Truthy/falsy shortcuts tab use karo jab behavior clearly samajh aaye.
Common beginner mistakes
=ko equality comparison samajhna.==aur===ko exactly same samajhna."10" + 5ka result 15 expect karna.Boolean("false")ko false expect karna.Number("")ka resultNaNexpect karna.NaN === NaNse NaN check karna instead ofNumber.isNaN().- Operator precedence guess karna without parentheses.
- Regular numbers aur BigInt directly mix karna.
Chapter checklist
- Arithmetic aur assignment operators use kar sakte ho?
- Comparison ka result boolean hota hai ye clear hai?
===vs==difference samajh aaya?&&,||,!aur??ka basic role clear hai?Number(),String()aurBoolean()use kar sakte ho?- Truthy/falsy aur implicit coercion ke common surprises recognize kar sakte ho?
Practice Task — Operator & Conversion Lab
Browser Console me ye exercises khud type karo.
20 + 5,20 - 5,20 * 5,20 / 5,20 % 6run karo.let score = 50banao aurscore += 10use karo.5 == "5"aur5 === "5"compare karo.- Three comparison expressions banao using
>,<and>=. true && false,true || falseaur!trueka output dekho.null ?? "Guest",0 ?? 100aur0 || 100compare karo.Number("25"),Number("25px"),parseInt("25px", 10)compare karo.Boolean(0),Boolean("0"),Boolean("")aurBoolean("false")run karo."10" + 5aurNumber("10") + 5compare karo.- Intentionally invalid numeric text convert karo aur
Number.isNaN()se result check karo.