A unit of code that can be evaluated to a value is called expression.
An expression in programming is a combination of variables, constants, operators, and function calls that produces a single value. It can represent a computation or a value, and it typically follows a specific syntax dictated by the programming language being used.
There are many types of expressions in JavaScript like:
An arithmetic expression in programming is a combination of operands and arithmetic operators that evaluates to a single numerical value. These expressions can involve variables, constants, and mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
// Arithmetic Expressions
// addition
var sum = 5 + 3; // 8
// Subtraction
var sum = 5 - 3; // 2
// multiplication
var product = 6 * 2; // 12
// Division
var quotient = 20 / 4; // 5
// Modulus
var remainder = 10 % 4; // 2
// Increment and Decrement
var counter = 3;
counter++; // 4
counter--; // 3
// Exponentiation
var cube = 2 ** 3 // 8
A relational expression in programming is a type of expression that evaluates to a boolean value based on the comparison of two entities. These entities can be variables, constants, or expressions themselves.
// Relational Expression
var num1 = 5;
var num2 = '5';
var num3 = 19;
// `==`
console.log(num1 == num2); // true
// `===`
console.log(num1 === num2); // false
// `!=`
console.log(num1 != num2); // false
// `!==`
console.log(num1 !== num2); // true
// `>`
console.log(num1 > num3) // false
// `<`
console.log(num1 < num3); // true
// `>=`
console.log(num1 >= num3); // false
// `<=`
console.log(num1 <= num3); // true
These expressions are used to represent logical conditions or statements and are typically composed of logical operators such as AND (&&), OR (||), and NOT (!).
// Logical Expression
var x = true;
var y = false;
// `&&`
console.log(x && y); // false
// `||`
console.log(x || y); // true
// `!`
console.log(!x); // false
Assignment operators are symbols used to assign values to variables
// Assignment Expression
var x = 45; // x is assigned the value 45
var y = "Coders"; // y is assigned the string "Coders"
// `+=`
var x = 5;
x += 3; // Output: 2
// `-=`
var x = 7;
x -= 4; // Output: 3
// `*=`
var x = 3;
x *= 2; // Output: 6
// `/=`
let x = 10;
x /= 2; // Output: 5
// `%=`
var x = 10;
x %= 3; // Output: 1