Expression Vs Statement
What is Expression?
Expression in javascript is a slot in the statement which output some value , without expression the statement is incomplete in javascript . Expression include ternary operators , passing value to variable , mutation in array by array methods , or some logical or relational calculation in the value.
What is Statement?
Statement in javascript which execute some instruction and called as a program . Statement is incomplete without a expression and carried a slot for the expression to fit in . Declaring a variable needs some value which is a expression that makes the complete statement.
Statement doesn't carries a value whether the expression is a value and combination of expression into the statement makes the program start its execution and it forms the complete statement.
Statements like if-else conditional statements , for loops , Declaring some variable , functions etc.
Expression as Statement
In the above paragraph , we understood that expression is a value and statement has the slot for the expression to execute in the file . But , if we write only expression and execute the expression . Will that expression execute ? No , it will never execute and also throw error as a syntax error . Because javascript programs are written in statement which also has expression and writing only expression will not make it statement.
Trick to find ( Expression or Statement)
There is a trick to find whether the particular code is expression or statement then you just need to console log the code , if it will execute and give some value in the log , then it is a expression or else it will throw the error , which is a conclusion to be a statement.
This is the best trick to find whether it's a expression or statement , just console.log() the code.
JSX in React
Jsx in react which is also called the javascript XML which allows us to write javascript in the html in the curly braces {} . It is also called javascript syntax extension which has some concept of javascript that it accepts only expression in the curly braces and not the statements . If you will write if-else conditional statements , for loops , functions then it will throw the error . In these , braces {} react only allow expressions to execute which is some value.
When you want to write some conditions , you use short circuiting , ternary operators for conditions that gives some value and operates like if-else conditions , array methods like map , filter , reduce which return some value will only execute in jsx.
We often blame react for these concepts , but they are the core concepts of javascript that beginners don't understand and directly jump to react language that makes them blame react for its operations.
Thank you for reading :)
Comments
Post a Comment