Intern
What do expressions do in JavaScript?
sobes.tech AI
Answer from AI
Expressions in JavaScript are fragments of code that are evaluated to obtain a value. They can be simple (literals) or complex (operations with variables and functions).
Examples of expressions:
- Assigning a value to a variable (
let x = 5;) - Mathematical operations (
a + b) - Function calls (
myFunction()) - Logical operations (
true && false) - Accessing object properties (
obj.property) - Creating literals (
"string",[1, 2],{ key: 'value' })
Expressions are often used inside operators, for example, in conditional statements (if (expression)) or loops (while (expression)).
// Examples of expressions
let a = 10; // Assignment expression
let b = a * 2; // Multiplication expression
let result = Math.max(a, b); // Function call expression