Logo

Expressions and Operators in JavaScript

Tue Sep 10 2024
Technology
35
0
0
5 days ago

A comprehensive guide to JavaScript expressions and operators, covering conditional, comparison, arithmetic, and bitwise operators, and more

Expressions and Operators in JavaScript

Expressions and operators are fundamental to writing any JavaScript program. They allow us to perform calculations, make decisions, assign values, and manipulate data. In this guide, we’ll explore the various operators available in JavaScript and their uses.


1. Conditional (Ternary) Operator

The conditional (ternary) operator is the only JavaScript operator that takes three operands. It provides a shorthand for if-else statements.

Syntax:

condition ? expression1 : expression2;

Example:

let age = 18;
let canVote = age >= 18 ? 'Yes' : 'No';
console.log(canVote); // Output: Yes

2. Comma Operator

The comma operator allows multiple expressions to be evaluated, returning the value of the last expression.

Syntax:

expression1, expression2, expression3;

Example:

let x = (1, 2, 3);
console.log(x); // Output: 3

3. Unary Operators

Unary operators work with a single operand. Examples include the increment (++), decrement (--), and negation (-) operators.

Example:

let x = 5;
x++; // Increment
console.log(x); // Output: 6

let y = -x; // Negation
console.log(y); // Output: -6

4. Relational Operators

Relational operators are used to compare two values. These include <, >, <=, and >=.

Example:

let x = 5;
let y = 10;
console.log(x > y);  // Output: false
console.log(x <= y); // Output: true

5. Assignment Operators

Assignment operators assign values to variables. The most common is the simple assignment (=), but there are also compound operators like += and -=.

Example:

let x = 10;
x += 5; // Equivalent to x = x + 5
console.log(x); // Output: 15

6. Comparison Operators

Comparison operators compare two values and return a boolean result. These include ==, ===, !=, and !==.

Example:

console.log(5 == '5');  // Output: true (loose equality)
console.log(5 === '5'); // Output: false (strict equality)

7. Arithmetic Operators

Arithmetic operators perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), and division (/).

Example:

let a = 10;
let b = 5;
console.log(a + b); // Output: 15
console.log(a / b); // Output: 2

8. Bitwise Operators

Bitwise operators treat their operands as a set of 32 bits and operate on them bit by bit. These include & (AND), | (OR), and ^ (XOR).

Example:

let x = 5;  // 0101 in binary
let y = 3;  // 0011 in binary
console.log(x & y); // Output: 1 (0001 in binary)

9. Logical Operators

Logical operators are used with boolean values. The three main logical operators are && (AND), || (OR), and ! (NOT).

Example:

let isTrue = true;
let isFalse = false;
console.log(isTrue && isFalse); // Output: false
console.log(isTrue || isFalse); // Output: true
console.log(!isTrue);           // Output: false

10. BigInt Operators

BigInt operators allow you to work with integers larger than the safe number limit of Number.MAX_SAFE_INTEGER. BigInt can be used with all arithmetic operators except ** (exponentiation).

Example:

let bigInt = 123456789012345678901234567890n;
let result = bigInt + 10n;
console.log(result); // Output: 123456789012345678901234567900n

11. String Operators

The string operator in JavaScript is the + operator, used to concatenate (combine) strings.

Example:

let greeting = "Hello" + " " + "World";
console.log(greeting); // Output: Hello World

Conclusion

Expressions and operators are the backbone of any JavaScript program, enabling us to perform a variety of tasks, from basic arithmetic to complex logical comparisons. By understanding how to use these operators effectively, you can make your code more efficient and expressive.