Expression and statement in JavaScript

Expression
An expression is a syntactic entity or unit of code that may be evaluated in order to determine its value. It is a combination of one or more constants, variables, functions, and operators (according to its particular rules of precedence and association).
examples:
var x = 7
x = 7
is an expression that uses the =
operator to assign the value seven to the variable x
. The expression itself evaluates to seven.
var x = a + b * 10
x = a + b * 10
is an expression that uses the =
operator to assign the valuea + b * 10
to the variable x
. In expression value , a
b
10
are operands and +
-
are operators.
The code 3 + 4
is also an example of expression type. This expression uses the +
operator to add three and four together without assigning the result, seven, to a variable.
Types of Expression
Arithmetic Expressions:
Arithmetic expressions evaluates to a number (Generally uses arithmetic operators.). Examples
1;
// Here 10 is an expression that is evaluated as number by the JS interpreter1+2;
// This is another expression that is evaluated to number 3
String Expression:
Evaluates to a character string. Examples
"Names"
"123"
"Java" + "Script" // evaluates to the string 'JavaScript'
Logical Expression:
Evaluates to true or false.
Logical AND (&&)
Logical OR (||)
Logical NOT (!)1<2
x==10
Primary expressions:
Basic keywords and general expressions in JavaScript ,these are constant or literal values, certain language keywords, and variable references.
1.23 // A number literal
"hello" // A string literal
/pattern/ // A regular expression literaltrue // Evalutes to the boolean true value
false // Evaluates to the boolean false value
null // Evaluates to the null value
this // Evaluates to the "current" objecti // Evaluates to the value of the variable i.
sum // Evaluates to the value of the variable sum.
undefined // undefined is a global variable, not a keyword like null.
Left-hand-side expressions:
Left values are the destination of an assignment
Statement
A statement performs an action(an instruction to tell the browser to what action to perform.). Loops and if
statements are examples of statements. 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.
JavaScript statements are composed of: Values, Operators, Expressions, Keywords, and Comments.
document.getElementById(“demo”).innerHTML = “JavaScript.”;//This statement tells the browser to write “JavaScript.” inside an HTML element with id=”demo”:
Conditional statement vs Conditional Expression:
if-else statements.
var salutation;
if (male) {
salutation = 'Mr.';
} else {
salutation = 'Mrs.';
}
Above Conditional statements example are equivalent to the following code that is Conditional Expression:
var salutation = (male ? 'Mr.' : 'Mrs.');
The code between the equals sign and the semicolon is an expression.
Function Declarations vs Function Expression:
Function Declaration : A function, declared as a separate statement, in the main code flow.
function sayHi() {
alert( "Hello" );
}
Function Expression: A function, created inside an expression or inside another syntax construct. It allows us to create a new function in the middle of any expression. A Function Declaration can be called earlier than it is defined. A Function Expression is created when the execution reaches it and is usable only from that moment.
For example: Here, the function is created at the right side of the “assignment expression” =
.
let sayHi = function() {
alert( "Hello" );
};
As the function creation happens in the context of the assignment expression (to the right side of =
), this is a Function Expression.