Home 〉 Javascript Tutorials 〉 JavaScript Operators Explained with Examples | Complete Guide
JavaScript Operators Explained with Examples | Complete Guide !
What are JavaScript operators, and how do they function in your code? This guide delves into the various types of JavaScript operators—arithmetic, comparison, logical, bitwise, string, and ternary—providing syntax, examples, and detailed explanations for each. You'll learn how to use these operators individually and in combination to perform complex operations. By the end, you'll have a solid understanding of how operators work in JavaScript, enhancing your coding proficiency. Let's explore the world of JavaScript operators!
In JavaScript, operators are symbols that perform operations on variables and values. They are essential for tasks like arithmetic calculations, comparisons, logical operations, and more. Understanding these operators is crucial for writing effective JavaScript code.
Arithmetic operators perform basic mathematical operations:
Example 📄
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1
console.log(a ** b); // 1000
a++;
console.log(a); // 11
b--;
console.log(b); // 2
These operators are fundamental for performing calculations in JavaScript.
Comparison operators compare two values and return a boolean result:
Example 📄
let x = 5;
let y = '5';
console.log(x == y); // true
console.log(x === y); // false
console.log(x != y); // false
console.log(x !== y); // true
console.log(x > 3); // true
console.log(x < 3); // false
Use these operators to compare values and make decisions in your code.
Logical operators are used to determine the logic between variables or values:
Example 📄
let a = true;
let b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false
Logical operators are essential for controlling the flow of your programs.
Bitwise operators perform operations on binary representations of numbers:
Example 📄
let a = 5; // 0101
let b = 3; // 0011
console.log(a & b); // 1 (0001)
console.log(a | b); // 7 (0111)
console.log(a ^ b); // 6 (0110)
console.log(~a); // -6
console.log(a << 1); // 10
console.log(a >> 1); // 2
Bitwise operators are useful for low-level programming tasks.
The + operator is also used for string concatenation:
Example 📄
let firstName = 'John';
let lastName = 'Doe';
console.log(firstName + ' ' + lastName); // John Doe
Use the + operator to combine strings.
The ternary operator is a shorthand for the if-else statement:
html syntax ✍
condition ? expressionIfTrue : expressionIfFalse;
Example 📄
let age = 18;
let status = (age >= 18) ? 'Adult' : 'Minor';
console.log(status); // Adult
The ternary operator is concise and useful for simple conditional assignments.
You can combine multiple operators to perform complex operations:
Example 📄
let a = 5;
let b = 10;
let c = 15;
let result = (a + b > c) && (b < c);
console.log(result); // true
In this example, (a + b > c) evaluates to true, and (b < c) is also true. The logical AND (&&) returns true because both conditions are true.
Understanding JavaScript operators is fundamental for writing efficient and effective code. From performing arithmetic calculations to making logical decisions, operators are integral to programming in JavaScript. Practice using these operators to become proficient in their application.
The == operator compares values for equality after converting both values to a common type, whereas === compares both value and type without conversion.
Yes, you can combine multiple operators in a single expression. Be mindful of operator precedence to ensure the expression evaluates as intended.
The modulus operator returns the remainder of a division operation. For example, 10 % 3 returns 1.