statement and expression these are two things that we Programmers deal with it every moment in the day.
It's impossible when you're writing code and don't use expressions or statements. But some of the developers aren't familiar with their differences. in this article, I am going to explain what expressions and statements are and why we use them a lot in our daily life as a programmer?🤔
Expression
In a simple word, expressions are a combination of Numbers, Symbols, and operators and the purpose of an expression is to produce a value. Let me give you an example in mathematics for example (32+67) is an expression because it produces a number (99) and containers (+) as an operator
expressions in computer programming mean the same expression in programming
let x =12
Console.log(x += 4)
output: 16
In the example above we define a variable and incremented by 4 and logged it on the console, in your opinion which part of the code above is an expression?🤔
😊Yes your answer is correct😊, (x += 4) is an expression because it produces the number 16 as a value
Statement
think of a computer program, a computer program contains a list of instructions in which to be executed by the computer.
in the programming languages, these instruction are called statement
actually, the statements can perform several operations like looping and
in a programming language like javascript, statements are composed of the following options:
1 - Values
2 - Operators
3 - Expressions (Expressions are also called statement)
4 - Keywords
5 - Comments
To understand better look at the following example
let a = 'hello'
let b = 'guys'
var demo = document.getElementbyId('demo')
demo.innerHTML = a +''+ b
in the code above 'dom' gets the HTML element(tag) by its ID
which is 'demo' and then set the HTML content to 'hello'
so why is it is a statement?🤔🤨
because the code contains an operator (=) and (a +''+ b ) is an expression because it produces a string ('hello guys') as a value
Another example of a statement is if...else in which is a conditional statement
var manner = 'happy'
if(manner == 'happy'){
return 'perfect😊'
}else{
return 'You are sad😢'
}
output: 'perfect😊
the (manner == 'happy') is an expression because it returns a boolean as a value if the condition is true it returns 'perfect😊'
otherwise returns 'You are sad😢'
Note :
1-expressions are the statements that produce a value
2-when a variable name is used in an expression the current value
3-statements are executed one by one or in the same order they are
written
I tried to explain the concepts well and understandable
I hope you enjoy reading this article
❤see you until the next article❤
Top comments (0)