Course Content
Data types and Values
0/1
Object-oriented programming in JavaScript
0/1
Error handling and debugging in JavaScript
0/1
JavaScript functions for string and array manipulation
0/1
JavaScript Libraries and Frameworks
0/1
JavaScript
About Lesson

Expressions

An expression is a combination of values, variables, operators, and function calls that can be evaluated to produce a result. Expressions are a fundamental concept in programming and are used to perform calculations, make decisions, and manipulate data. They represent the building blocks of more complex operations and algorithms.

 

Here’s a breakdown of key components and aspects of expressions in coding:

Component Description
Values Expressions often involve values, which can be numbers, strings, boolean values (true or false), or other data types. These values serve as the operands of various operators.
Variables Variables are placeholders for storing values. Expressions can include variables, and their values are substituted into the expression during evaluation. This allows for dynamic calculations and manipulation of data.
Operators Operators are symbols that perform operations on one or more operands. There are various types of operators, such as arithmetic operators (+, -, *, /), comparison operators (==, >, <), logical operators (&&, ||), and more. Operators define how values and variables interact within an expression.
Functions Expressions can involve function calls, where a function takes input arguments and returns a value. Functions can be used to perform complex operations and calculations, encapsulating reusable pieces of code.
Combining Elements Expressions allow you to combine values, variables, operators, and function calls to create more complex calculations and evaluations. For example, the expression 3 + x * (y – 2) combines numeric values, variables, and arithmetic operators to produce a result based on the current values of x and y.
Evaluating Expressions When a program encounters an expression, it evaluates it by following the operator precedence rules and substituting variable values. The result of the expression is a single value that can be used in further calculations or as part of the program’s logic.

Examples of Expressions in JavaScript

The statement below has four expressions in it:

    a = b * 2;
  
  • 2 is called a literal value expression.
  • b is called a variable expression, which means the code has to retrieve the variables current value.
  • b * 2 is an arithmetic expression, which means to do the multiplication.
  • a = b * 2 is an assignment expression, indicating that the result of the b * 2 expression is assigned to the variable a.

An expression that stands independently is termed an expression statement:

    b * 2;
  

This type of expression statement is infrequently used and lacks practicality, as it generally doesn’t influence the program’s execution. It retrieves the value of the variable b and multiplies it with the number 2, but, it does nothing with the result.

Arithmetic Expression

    let x = 5;
    let y = 10;
    let result = x + y * 2;
    console.log(result); // Output: 25
  

String Concatenation

    let firstName = "John";
    let lastName = "Doe";
    let fullName = firstName + " " + lastName;
    console.log(fullName); // Output: John Doe
  

Comparison and Logical Expression

    let age = 25;
    let isAdult = age >= 18 && age <= 65;
    console.log(isAdult); // Output: true
  

Function Call Expression

    let radius = 5.0;
    let area = Math.PI * Math.pow(radius, 2);
    console.log(area); // Output: 78.53981633974483