On day 6(link) we dived into the scope of javascript and how javascript variables are visible or accessible in javascript programs. Specifically, it determines where a variable or function can be accessed and how long it remains in memory.
Today we dive into expressions vs statements in Javascript so we will first have to know the dictionary definition of the two words.
Expression(declaration) - The action of making known one's thoughts or feelings.
Statement(assertion) - A definite or clear expression of something in speech or writing.
What are there in Javascript?
Javascript Expression
In JavaScript, an expression is a piece of code that produces a value. It can be a simple value, like a number or string, or a more complex value, like an object or array.
Some examples of JavaScript expressions are:
Literal Values:
5
,"Hello, world!"
,true
,null
,undefined
.Variables:
x
,y
,firstName
,lastName
.Arithmetic Expressions:
2 + 3
,x * y
,10 / z
.Comparison Expressions:
x < y
,firstName === lastName
.Logical Expressions:
x && y
,x || y
.Function Calls:
square(5)
,console.log("Hello, world!")
.Array and Object Expressions:
[1, 2, 3]
,{ name: "John", age: 30 }
.
JavaScript expressions can be used in a variety of ways, such as:
Assigning values to variables:
let x = 2 + 3;
Passing arguments to functions:
console.log(x * y);
Comparing values:
if (x < y) { /* do something */ }
Returning values from functions:
function square(x) { return x * x; }
Creating objects and arrays:
let person = { name: "John", age: 30 };
Understanding expressions is fundamental to writing JavaScript code, as they form the basis of many programming concepts, such as variables, functions, and operators.
Javascript Statements
In JavaScript, a statement is a piece of code that performs a specific action. It does not produce a value like an expression does, but instead instructs the computer to do something. There are many types of statements in JavaScript, including:
- Variable Declarations:
let x = 10;
const PI = 3.14159;
Variable Declarations are considered a statement in JavaScript because they are an action that instructs the computer to create a variable and optionally assign a value to it. The process of declaring a variable reserves memory space for the variable in the computer's memory, which is an action that cannot be done through an expression.
- Conditional Statements:
if (x < y) {
// do something if x is less than y
} else {
// do something else if x is not less than y
}
Conditional Statements are considered statements in JavaScript because they are an action that instructs the computer to make a decision based on a condition. The condition is an expression that produces a Boolean value (true or false), and the decision determines which block of code to execute based on the value of the condition.
- Loop Statements:
for (let i = 0; i < 10; i++) {
// do something 10 times
}
while (x < y) {
// do something while x is less than y
}
Loop Statements are considered statements in JavaScript because they are an action that instructs the computer to repeat a block of code multiple times. The loop statement consists of a condition that determines whether to continue the loop or exit it, and a block of code to execute on each iteration of the loop.
- Function Declarations:
function sayHello(name) {
console.log("Hello, " + name + "!");
}
Function Declarations are considered statements in JavaScript because they are an action that instructs the computer to create a new function with a given name and body. The function declaration defines the function and makes it available for use in the program.
- Class Declarations:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
}
}
Class Declarations are considered statements in JavaScript because they are an action that instructs the computer to create a new class with a given name and body. The class declaration defines the class and makes it available for use in the program.
- Expression Statements:
a + b;
console.log("Hello, world!");
In this example, you are telling the computer to evaluate the expression "Hello, world!"
and pass it as an argument to the console.log()
function. The console.log()
function then prints the value of the expression to the console.
Expression Statements are considered statements in JavaScript because they are an action that instructs the computer to evaluate an expression and possibly do something with the resulting value. The expression statement consists of an expression followed by a semicolon.
These are just a few examples of statements in JavaScript, but there are many more. Statements are fundamental to JavaScript programming and are used to control the flow of execution in a program. Understanding statements is essential for writing functional, efficient, and easy-to-read JavaScript code.
Difference between Javascript expressions&Statements
JavaScript Expressions | JavaScript Statements |
Evaluates to a value | Performs an action |
Can be used as part of a statement | Cannot be used as part of an expression |
Examples: 2 + 2, "Hello" + "World", x = 5 | Examples: if statement, for loop, the switch statement |
Can be used as a function argument or returned value | Cannot be used as a function argument or returned value |
Can be assigned to a variable | Cannot be assigned to a variable |
Examples: x + y, 3 * z, "Hello" + name | Examples: if (condition) { code block }, while (condition) { code block }, for (initialization; condition; update) { code block } |
Cannot contain control flow statements | Can contain control flow statements |
Conclusion
In summary, expressions in JavaScript evaluate to a value, can be used as part of a statement, can be used as a function argument or returned value, and can be assigned to a variable.
Statements, on the other hand, perform an action, cannot be used as part of an expression, cannot be used as a function argument or returned value, and cannot be assigned to a variable. Control flow statements, such as break, continue, and return, can only be used in statements.
I hope you now understand the difference between expressions and statements in Javascript.
Please subscribe to our newsletter and we promise we won't spam your inbox. You will get the best resources about javascript and programming in general.
You can follow our 30in30 series using this link where we learn 30 concepts in 30 days.