Javascript Expressions
In JavaScript, an expression is a unit of code that evaluates to a single value. This value can be of any data type, such as a number, string, boolean, object, or function.
Literals
Example
Run Code5; // A number literal, evaluates to 5
"hello"; // A string literal, evaluates to "hello"
true; // A boolean literal, evaluates to true
null; // The null literal, evaluates to null
Variable References
Example
Run Codelet x = 10;
x; // Evaluates to the value of x, which is 10
Arithmetic Expressions
Example
Run Code5 + 3; // Evaluates to 8
10 * 2; // Evaluates to 20
(15 - 5) / 2; // Evaluates to 5
String Concatenation
Example
Run Code"Hello" + " " + "World"; // Evaluates to "Hello World"
Comparison Expressions
Example
Run Code5 > 3; // Evaluates to true
"apple" === "orange"; // Evaluates to false
Logical Expressions
Example
Run Codetrue && false; // Evaluates to false
(10 > 5) || (20 < 10); // Evaluates to true
Function Calls
Example
Run Codefunction add(a, b) {
return a + b;
}
add(2, 3); // Evaluates to 5
Function Expressions
Example
Run Codelet greet = function() {
return "Greetings!";
};
greet(); // Evaluates to "Greetings!"
Ternary Operator
Example
Run Codelet age = 18;
let status = (age >= 18) ? "Adult" : "Minor"; // Evaluates to "Adult"
Array and Object Literals
Example
Run Code[1, 2, 3]; // Evaluates to an array
{ name: "Alice", age: 30 }; // Evaluates to an object