JavaScript Operators and Expressions
- Published on
Introduction
Understanding operators and expressions is crucial for effectively manipulating data and controlling the flow of code execution in JavaScript. Operators perform operations on variables and values, while expressions represent formulas that compute values. This guide provides a comprehensive look into the various operators available in JavaScript and how expressions are evaluated.
Arithmetic Operators
Arithmetic operators perform mathematical operations:
let a = 10;
let b = 20;
console.log(a + b); // 30
console.log(a - b); // -10
console.log(a * b); // 200
console.log(b / a); // 2
console.log(b % a); // 0
Assignment Operators
Assignment operators assign values to variables:
let x = 5;
x += 10; // x = x + 10
x -= 3; // x = x - 3
x *= 2; // x = x * 2
Comparison Operators
Comparison operators compare two values and return a boolean result:
console.log(10 == "10"); // true
console.log(10 === "10"); // false
console.log(10 != "10"); // false
console.log(10 !== "10"); // true
console.log(5 > 3); // true
console.log(5 < 3); // false
Logical Operators
Logical operators perform logical operations and are often used with boolean values to write complex logical conditions:
console.log(true && false); // false
console.log(true || false); // true
console.log(!true); // false
String Operators
The +
operator is used to concatenate strings:
let greeting = "Hello" + " " + "World!";
console.log(greeting); // Hello World!
Ternary Operator
The ternary operator is a shorthand for the if
statement and is also known as the conditional operator:
let age = 20;
let type = (age >= 18) ? 'Adult' : 'Minor';
console.log(type); // Adult
Understanding Expressions
Expressions in JavaScript are evaluated to produce a value. They are combinations of variables, literals, operators, and expressions that are interpreted and computed by JavaScript:
let total = 10 + 20 * 3; // 70
let output = total > 50 ? 'High' : 'Low'; // High
Conclusion
JavaScript offers a wide array of operators to perform operations and manipulate data effectively. Understanding how these operators work and how expressions are evaluated allows developers to write more efficient and readable code. Moving forward, try to utilize these operators in various combinations and contexts to deepen your understanding and mastery of JavaScript.