From 7a6f8dd6e2a6e670e8806ba3f80922c7b0bf131b Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Sun, 10 Oct 2021 12:32:58 +0300 Subject: [PATCH] Add JS expression/statement --- blog_posts/javascript-expression-statement.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 blog_posts/javascript-expression-statement.md diff --git a/blog_posts/javascript-expression-statement.md b/blog_posts/javascript-expression-statement.md new file mode 100644 index 000000000..717c01246 --- /dev/null +++ b/blog_posts/javascript-expression-statement.md @@ -0,0 +1,25 @@ +--- +title: What is the difference between an expression and a statement in JavaScript? +type: question +tags: javascript,type +authors: chalarangelo +cover: blog_images/forest-balcony.jpg +excerpt: JavaScript distinguishes expressions and statements. Learn their differences in this short article. +firstSeen: 2021-11-07T05:00:00-04:00 +--- + +JavaScript distinguishes expressions and statements. An **expression** is any valid unit of code that resolves to a value. A **statement** is a unit of code that performs an action. Some examples: + +```js +// Statements +let x = 0; +function add(a, b) { return a + b; } +if (true) { console.log('Hi'); } + +// Expressions +x; // Resolves to 0 +3 + x; // Resolves to 3 +add(1, 2); // Resolves to 3 +``` + +Anywhere JavaScript expects a statement, you can also write an expression. This kind of statement is called an **expression statement**. Conversely, you cannot write a statement where JavaScript expects an expression.