statement and expression are terms used to describe javascript codes, it is important that we understand these two distinct concepts. Javascript as a language makes distinction between statement and expression.
Expression
An expression is any line of code that produce a value. Since expressions produce values, they can appear anywhere in a program where JavaScript expects a value such as the arguments of a function invocation.
In javascript, expression are grouped into the following categories:
Arithmetic Expressions:
Arithmetic expressions evaluate to a numeric value. Examples include the following
13; // Here 13 is an expression that is evaluated to the numeric value 13 by the Javascript interpreter
10+13; // This is another expression that is evaluated to produce the numeric value 23
String Expressions:
String expressions are expressions that evaluate to a string. Examples include the following
"Hi";
"Hi" + Isaac; // evaluates to the string "Hi Isaac"
Boolean expressions:
Boolean expressions are expressions that evaluate to true or false. Examples includes the following:
true; //evaluates to boolean value true
false; // evaluates to boolean value false
10 > 5; // evaluates to boolean value true
10 < 15; // evaluates to boolean value false
Left-hand-side Expressions
left-hand-side expressions are those that can appear on the left side of an assignment expression.
They include: values of variable, properties of object, elements of arrays and so on.
Examples of left-hand-side expressions include the following
// value of variables such as i
i = 10;
// properties of objects
var obj = {}; // an empty object with no properties
// elements of arrays
array[0] = 20;
Statements
A statement is an instruction to perform a specific action. Such actions include creating a variable or a function, looping through an array of elements, evaluating code based on a specific condition and so on. JavaScript programs are actually a sequence of statements.
Statements in JavaScript can be classified into the following categories:
Expression Statements:
Wherever JavaScript expects a statement, you can also write an expression. Such statements are referred to as expression statements. But the reverse does not hold. You cannot use a statement in place of an expression.
var a = (b = 1); // since (b = 1) is an assignment expression and not a statement, this is a perfectly acceptable line of code
console.log(var a); // results in error as you can pass only expressions as a function argument
var a = var b; // leads to an error because you cannot use a statement in the place of an expression
Declaration Statements:
Declaration statements create variables and functions by using the var and function statements respectively. Examples include:
// A function declaration statement
function greet(message) {
console.log(message);
}
Conditional Statements:
Conditional statements execute statements based on the value of an expression. Examples of conditional statements includes the if..else and switch statements.
// Syntax of an if statement.
if (expression) {
statement 1} else {
statement 2
}
Expression vs Statement:
If expressions are constructs that stand for values, then typically statements are constructs that control the execution of the program. All expressions are statements but not vice a versa. As Dr. Axel said:
A program is basically a sequence of statements. Wherever JavaScript expects a statement, you can also write an expression. Such a statement is called an expression statement. The reverse does not hold: you cannot write a statement where JavaScript expects an expression. For example, an if statement cannot become the argument of a function.
Expressions are composable and reducible. Composable means smaller expressions can combine in various ways to generate bigger expressions. Reducible means complex expressions can be simplified and optimized to yield better performance either by programmer, compiler or runtime engine.