In JavaScript, expressions are pieces of code that evaluate to a value. They can be as simple as a single value or as complex as a combination of operators, variables, and functions. Here are some examples of different types of expressions in JavaScript:
Literal Expressions: These are the simplest form of expressions that represent fixed values.
javascr5 // Number literal "Hello" // String literal true // Boolean literal
Arithmetic Expressions: These use arithmetic operators to compute values.
javascript5 + 3 // Addition, results in 8 10 - 4 // Subtraction, results in 6 7 * 2 // Multiplication, results in 14 20 / 4 // Division, results in 5
String Expressions: These involve string concatenation or interpolation.
javascript"Hello, " + "world!" // Concatenation, results in "Hello, world!" `The sum is ${5 + 3}` // Template literal, results in "The sum is 8"
Comparison Expressions: These compare values and return a Boolean result.
javascript5 > 3 // Greater than, results in true 10 === 10 // Strict equality, results in true 4 !== 5 // Not equal, results in true
Logical Expressions: These use logical operators to combine Boolean values.
javascripttrue && false // Logical AND, results in false true || false // Logical OR, results in true !true // Logical NOT, results in false
Assignment Expressions: These assign values to variables.
javascriptlet x = 10; // Assignment expression, x is assigned the value 10 x += 5; // Compound assignment, x is updated to 15
Function Expressions: These involve functions and can be used to define anonymous functions.
javascriptlet add = function(a, b) { return a + b; }; // Anonymous function assigned to variable add(2, 3); // Function call, results in 5
In essence, any piece of code that produces a value when evaluated is considered an expression in JavaScript.
No comments:
Post a Comment