Expressions vs Statements
Published on
Last updated on
1 min read • --- views
JavaScript distinguishes expressions and statements. A statement is an instruction, an action.
Remember conditions with if
, loops
with while
and for
— all those are statements, because they just perform
actions and control actions, but don't become values.
Expression
returns (expresses) a value. Each of the following lines contains an expression:
100 // this is a literal that expresses number 100
getUserName() // expresses Serhii
5 + 2 // expresses 7
Statements
produce or control actions, but do not turn into values:
let x; // declare a variable 'x'
function foo() {} // declare a function 'foo'
function bar() {
return null // return is also a statement
}
You can't put statements where expressions are expected. For example, passing a const statement as a function argument will produce an error. Or trying to assign the if statement to a variable:
let b = if (x > 10) { return 100; }; // error!
console.log(const y); // error!
Related posts
You might also like:
Dispatch table in javascript
--- views
Polymorphism and Dispatch Tables in JS
What is Astro Framework?
--- views
Astro is a new Javascript framework, so discover what makes it special and what are its core features.
Mastering HTML Class Naming: Boosting CSS Efficiency
--- views
Discover the art of crafting clean and organized HTML class names while enhancing CSS naming conventions. Learn how to create maintainable, scalable, and SEO-friendly code with the latest best practices.
Share it: