➕ Arithmetic operator
An arithmetic operator accepts numerical values as operands and returns a single numerical value. The numerical values can be literals or variables.
⚡️ Addition operator (+)
The addition operator returns the sum of two values.
The following uses the addition operator to concatenate two strings:
let sum = 10 + 20;
console.log(sum); // 30
let x = '10',
y = '20';
let result = x + y;
console.log(result); //1020⚡️Subtraction operator (-)
For example:
And If a value is a string, a boolean, null, or undefined, the JavaScript engine will:
First, convert the value to a number using the
Number()function.Second, perform the subtraction.
let result = 30 - 10;
console.log(result);// 20⚡️Multiplication operator (*)
JavaScript uses the asterisk (*) to represent the multiplication operator. The multiplication operator multiplies two numbers and returns a single value. For example:
If either value is not a number, the JavaScript engine implicitly converts it into a number using the Number() function and perform the multiplication.
let result = 2 * 3;
console.log(result); //6let result = '5' * 2;
console.log(result); //10⚡️ Divide operator (/)
Javascript uses the slash (/) character to represent the divide operator. The divide operator divides the first value by the second one. For example:
let result = 20 / 10;
console.log(result);// 2If either value is not a number, the JavaScript engine converts it into a number for division. For example:
let result = '20' / 2;
console.log(result);// 10;⚡️ Modulus operator (%)
The addition operator returns the sum of two values.
The following uses the addition operator to concatenate two strings:
let mod = 10 % 20;
console.log(mod); // 10🟰 Assignment operator
Assignment operator(=) are used to assign values to variables in JavaScript.
x = 10
y = 20
x = y ;
console.log(x); //20
console.log(y); //20⚡️Addition Assignment Operator(+=)
The Addition assignment operator adds the value to the right operand to a variable and assigns the result to the variable. Addition or concatenation is possible. In case of concatenation then we use the string as an operand.
let a = 2;
const b = 3;
console.log(a += b ); //5⚡️Subtraction Assignment Operator(-=)
The Subtraction Assignment Operator subtracts the value of the right operand from a variable and assigns the result to the variable.
let y = 4;
console.log(y -= 1); //3⚡️Remainder Assignment Operator(%=)
The Remainder Assignment Operator divides a variable by the value of the right operand and assigns the remainder to the variable.
let Rem = 50;
console.log(Rem %= 10); //0⚡️Exponentiation Assignment Operator(**=)
The Exponentiation Assignment Operator raises the value of a variable to the power of the right operand.
let Exp = 2;
console.log(Exp **= 3); //8⚡️Division Assignment Operator(/=)
The Division Assignment operator divides a variable by the value of the right operand and assigns the result to the variable.
let Div = 10;
const m = 2;
console.log(Div /= m); //5⚡️Multiplication Assignment Operator(*=)
✍️ The Multiplication Assignment operator multiplies a variable by the value of the right operand and assigns the result to the variable.
let M = 4;
console.log(M *= 2); //8⚖️ Comparison Operators
⚡️Equality Operator (==)
The Equality operator is used to compare the equality of two operands.
let x = 55;
let y = 55;
console.log(x == 5); //true
console.log(y == 5); //true
console.log(x == y); //true⚡️Inequality Operator (!=)
The Inequality Operator is used to compare the inequality of two operands.
let x = 9;
let y = 7;
console.log(x != 6); //true
console.log(y != 7); //false
console.log(x != y); //false⚡️Strict equality Operator (===)
The Strict equality Operator is used to compare the equality of two operands with type.
let x = 33;
let y = 48;
console.log(x === 63); //false
console.log(y === 48); //true
console.log(x === y); //false⚡️Strict inequality Operator (!==)
The Strict inequality Operator is used to compare the inequality of two operands with type.
let x = 5;
let y = 5;
console.log(x !== 6); //true
console.log(y !== 5); //false
console.log(x !== y); //true⚡️Greater than Operator (>)
The Greater than Operator is used to check whether the left-side value is greater than the right-side value.
let x = 2;
let y = 3;
console.log(x > 16); //false
console.log(y > 15); //false⚡️Greater than or equal Operator (>=)
The Greater than or equal Operator is used to check whether the left side operand is greater than or equal to the right side operand.
let x = 1;
let y = 4;
console.log(x >= 5); //true
console.log(y >= 15); //false⚡️Less than Operator (<)
The Less than Operator is used to check whether the left-side value is less than the right-side value.
let x = 6;
let y = 8;
console.log(x < 5); //true
console.log(y < 15); //true⚡️Less than or equal Operator (<=)
The Less than or equal Operator is used to check whether the left side operand value is less than or equal to the right side operand value.
let val1 = 5;
let val2 = "5";
console.log(val1 <= 15); //true
console.log(val2 <= "0"); //false‼ Logical Operators
⚡️ Logical NOT operator (!)
JavaScript uses an exclamation point ! to represent the logical NOT operator. The ! operator can be applied to a single value of any type, not just a Boolean value.
When you apply the ! operator to a boolean value, the ! returns true if the value is false and vice versa. For example:
let eligible = false,
required = true;
console.log(!eligible); //true
console.log(!required); //falseThe logical ! operator works based on the following rules:
If
aisundefined, the result istrue.If
aisnull, the result istrue.If
ais a number other than0, the result isfalse.If
aisNaN, the result istrue.If
ais an object, the result is false.If
ais an empty string, the result istrue. Ifais a non-empty string, the result isfalse
⚡️Double-negation (!!)
Sometimes, you may see the double negation (!!) in the code. The !! uses the logical NOT operator (!) twice to convert a value to its real boolean value. For example:
let counter = 10;
console.log(!!counter);// trueThe first
!operator negates the Boolean value of thecountervariable. If thecounteristrue, then the!operator makes it false and vice versa.The second
!operator negates the result of the first!operator and returns the real boolean value of thecountervariable.
⚡️Logical AND operator (&&)
JavaScript uses the double ampersand (&&) to represent the logical AND operator.
The result of the && operator is true only if both values are true, otherwise, it is false. For example:
let eligible = false,
required = true;
console.log(eligible && required); // falselet eligible = true,
required = true;
console.log(eligible && required); // true⚡️The chain of && operators
The following expression uses multiple && operators:
let result = value1 && value2 && value3;The && operator carries the following:
Evaluates values from left to right.
For each value, convert it to a boolean. If the result is
false, stops and returns the original value.If all values are truthy values, return the last value
⚡️Logical OR operator (||)
JavaScript uses the double-pipe || to represent the logical OR operator.
The || operator returns false if both values are evaluated to false. In case either value is true, the || operator returns true. For example:
let eligible = true,
required = false;
console.log(eligible || required); // true⚡️The chain of || operators
The following example shows how to use multiple || operators in an expression:
let result = value1 || value2 || value3;The || operator does the following:
Evaluates values from left to right.
For each value, convert it to a boolean value. If the result of the conversion is
true, stops and returns the value.If all values have been evaluated to
false, returns the last value.
<div class="infographic-jsops">
<style>
.infographic-jsops {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--ok: #1a7f4e;
--ok-soft: #e3f5ec;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
max-width: 780px;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-jsops[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--ok: #5cd6a0;
--ok-soft: #16342a;
}
.infographic-jsops * { box-sizing: border-box; }
.infographic-jsops .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jsops .ig-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4.5vw, 1.9rem);
font-weight: 600; text-align: center; margin: 0 0 1.25rem;
}
.infographic-jsops .tabs { display: flex; gap: 0.4rem; justify-content: center; flex-wrap: wrap; }
.infographic-jsops .tab {
font: inherit; font-size: 0.8rem; font-weight: 600; border: 1px solid var(--line);
background: var(--paper); color: var(--ink-soft); padding: 0.4rem 0.9rem;
border-radius: 999px; cursor: pointer;
}
.infographic-jsops .tab.is-active { background: var(--accent); color: var(--paper); border-color: var(--accent); }
.infographic-jsops .chips { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: center; margin-top: 1rem; }
.infographic-jsops .chip {
font: inherit; font-size: 0.85rem; font-weight: 700; font-family: var(--mono-font);
border: 1px solid var(--line); background: var(--paper-alt); color: var(--ink);
padding: 0.4rem 0.85rem; border-radius: 6px; cursor: pointer;
}
.infographic-jsops .chip.is-active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.infographic-jsops .playground {
margin-top: 1.5rem; background: var(--paper-alt); border: 1px solid var(--line);
padding: 1.5rem 1.25rem;
}
.infographic-jsops .inputs-row { display: flex; align-items: flex-start; gap: 0.6rem; justify-content: center; flex-wrap: wrap; }
.infographic-jsops .val-input {
width: 5.5rem; height: 2.7rem; font-family: var(--mono-font); font-size: 1rem; text-align: center;
padding: 0 0.4rem; border: 1px solid var(--line); background: var(--paper);
color: var(--ink); outline: none;
}
.infographic-jsops .val-input:focus { border-color: var(--accent); }
.infographic-jsops .op-symbol {
font-family: var(--mono-font); font-size: 1.3rem; font-weight: 700; color: var(--accent);
min-width: 2.2rem; height: 2.7rem; display: flex; align-items: center; justify-content: center;
}
.infographic-jsops .eq-sign {
font-family: var(--mono-font); font-size: 1.1rem; color: var(--ink-soft);
height: 2.7rem; display: flex; align-items: center; justify-content: center;
}
.infographic-jsops .result-box {
font-family: var(--mono-font); font-size: 1.15rem; font-weight: 700;
height: 2.7rem; display: flex; align-items: center; justify-content: center;
padding: 0 1rem; background: var(--ok-soft); color: var(--ok);
border: 1px solid var(--ok); border-radius: 6px; min-width: 5rem;
box-shadow: 0 3px 6px rgba(0,0,0,0.12);
}
.infographic-jsops .result-type {
font-size: 0.68rem; color: var(--ink-soft); text-align: center; margin-top: 0.4rem;
}
.infographic-jsops .caption {
text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin-top: 1.1rem; min-height: 1.2em;
}
.infographic-jsops .caption strong { color: var(--ink); font-family: var(--mono-font); }
@media (max-width: 480px) {
.infographic-jsops .inputs-row { gap: 0.4rem; }
.infographic-jsops .val-input { width: 4.4rem; }
}
@media (prefers-reduced-motion: reduce) {
.infographic-jsops *, .infographic-jsops *::before, .infographic-jsops *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">JavaScript Operators</h2>
<div class="tabs" id="tabs"></div>
<div class="chips" id="chips"></div>
<div class="playground">
<div class="inputs-row" id="inputsRow"></div>
</div>
<p class="caption" id="caption"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jsops')
|| document.querySelector('.infographic-jsops');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const CATEGORIES = {
Arithmetic: [
{ sym: '+', caption: 'adds numbers, concatenates strings' },
{ sym: '-', caption: 'subtracts, converts non-numbers first' },
{ sym: '*', caption: 'multiplies, converts non-numbers first' },
{ sym: '/', caption: 'divides, converts non-numbers first' },
{ sym: '%', caption: 'remainder after division' }
],
Assignment: [
{ sym: '+=', caption: 'a becomes a + b' },
{ sym: '-=', caption: 'a becomes a - b' },
{ sym: '*=', caption: 'a becomes a * b' },
{ sym: '/=', caption: 'a becomes a / b' },
{ sym: '%=', caption: 'a becomes a % b' },
{ sym: '**=', caption: 'a becomes a raised to b' }
],
Comparison: [
{ sym: '==', caption: 'loose equality — allows coercion' },
{ sym: '!=', caption: 'loose inequality — allows coercion' },
{ sym: '===', caption: 'strict equality — checks type too' },
{ sym: '!==', caption: 'strict inequality — checks type too' },
{ sym: '>', caption: 'left greater than right' },
{ sym: '>=', caption: 'left greater than or equal' },
{ sym: '<', caption: 'left less than right' },
{ sym: '<=', caption: 'left less than or equal' }
],
Logical: [
{ sym: '!', unary: true, caption: 'flips truthy to false, and back' },
{ sym: '!!', unary: true, caption: 'converts any value to real boolean' },
{ sym: '&&', caption: 'true only if both are truthy' },
{ sym: '||', caption: 'true if either is truthy' }
]
};
function compute(sym, a, b) {
switch (sym) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '%': return a % b;
case '+=': return a + b;
case '-=': return a - b;
case '*=': return a * b;
case '/=': return a / b;
case '%=': return a % b;
case '**=': return a ** b;
case '==': return a == b;
case '!=': return a != b;
case '===': return a === b;
case '!==': return a !== b;
case '>': return a > b;
case '>=': return a >= b;
case '<': return a < b;
case '<=': return a <= b;
case '!': return !a;
case '!!': return !!a;
case '&&': return Boolean(a && b);
case '||': return Boolean(a || b);
}
}
function parseValue(str) {
if (str === undefined || str.trim() === '') return undefined;
const t = str.trim();
if (t === 'null') return null;
if (t === 'true') return true;
if (t === 'false') return false;
if (t === 'undefined') return undefined;
if (t !== '' && !isNaN(Number(t))) return Number(t);
return t;
}
function formatVal(v) {
if (typeof v === 'string') return JSON.stringify(v);
return String(v);
}
const tabsEl = wrapper.querySelector('#tabs');
const chipsEl = wrapper.querySelector('#chips');
const inputsRow = wrapper.querySelector('#inputsRow');
const caption = wrapper.querySelector('#caption');
let currentCategory = 'Arithmetic';
let currentOp = CATEGORIES.Arithmetic[0];
let aStr = '10', bStr = '20';
function buildInputs() {
inputsRow.innerHTML = '';
if (!currentOp.unary) {
const aInput = document.createElement('input');
aInput.className = 'val-input'; aInput.value = aStr;
aInput.addEventListener('input', () => { aStr = aInput.value; update(); });
inputsRow.appendChild(aInput);
}
const opEl = document.createElement('span');
opEl.className = 'op-symbol';
opEl.textContent = currentOp.unary ? currentOp.sym : currentOp.sym;
inputsRow.appendChild(opEl);
if (!currentOp.unary) {
const bInput = document.createElement('input');
bInput.className = 'val-input'; bInput.value = bStr;
bInput.addEventListener('input', () => { bStr = bInput.value; update(); });
inputsRow.appendChild(bInput);
} else {
const aInput = document.createElement('input');
aInput.className = 'val-input'; aInput.value = aStr;
aInput.addEventListener('input', () => { aStr = aInput.value; update(); });
inputsRow.appendChild(aInput);
}
const eq = document.createElement('span');
eq.className = 'eq-sign'; eq.textContent = '=';
inputsRow.appendChild(eq);
const resultWrap = document.createElement('div');
const resultBox = document.createElement('div');
resultBox.className = 'result-box'; resultBox.id = 'resultBox';
resultWrap.appendChild(resultBox);
const resultType = document.createElement('div');
resultType.className = 'result-type'; resultType.id = 'resultType';
resultWrap.appendChild(resultType);
inputsRow.appendChild(resultWrap);
}
function update() {
const a = parseValue(aStr);
const b = currentOp.unary ? undefined : parseValue(bStr);
let result;
try {
result = compute(currentOp.sym, a, b);
} catch (e) {
result = 'Error';
}
const resultBox = wrapper.querySelector('#resultBox');
const resultType = wrapper.querySelector('#resultType');
resultBox.textContent = formatVal(result);
resultType.textContent = typeof result;
caption.innerHTML = `<strong>${currentOp.sym}</strong> — ${currentOp.caption}`;
}
function selectOp(op) {
currentOp = op;
wrapper.querySelectorAll('.chip').forEach(c => c.classList.toggle('is-active', c.dataset.sym === op.sym));
buildInputs();
update();
}
function buildChips() {
chipsEl.innerHTML = '';
CATEGORIES[currentCategory].forEach((op, idx) => {
const chip = document.createElement('button');
chip.className = 'chip';
chip.textContent = op.sym;
chip.dataset.sym = op.sym;
chip.addEventListener('click', () => selectOp(op));
chipsEl.appendChild(chip);
if (idx === 0) selectOp(op);
});
}
function selectCategory(name) {
currentCategory = name;
wrapper.querySelectorAll('.tab').forEach(t => t.classList.toggle('is-active', t.dataset.cat === name));
buildChips();
}
Object.keys(CATEGORIES).forEach((name, idx) => {
const tab = document.createElement('button');
tab.className = 'tab';
tab.textContent = name;
tab.dataset.cat = name;
tab.addEventListener('click', () => selectCategory(name));
tabsEl.appendChild(tab);
});
selectCategory('Arithmetic');
})();
</script>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});