An expression is any valid set of literals, variables, operators and expressions that evaluates to a single value. We distinguish expressions to
- Assignment Expressions -
x = 7
- Evaluation expressions -
3 + 4
- commonly written in curly brackets (x + y)
While what our goal behind these two expressions is different, it doesn't matter if they consist of 3 characters or a code that goes to 40 lines*, in the end, they all evaluate to a single decisive value, be it certain primitive value (number
, string
, boolean
, null
, undefined
, symbol
) or an object
Meanwhile, a declaration is well.. precisely as a name suggests.. a declaration of something
var x;
declares an undefined
variable of name x
function
s in javascript are simply sub-types of the object
data type. So by doing
function foo (){}
We simply declare an object (function) that to the top of the module.
Keep in mind, declaration and expressions do not have to be some sort of separate techniques that may never be combined, in fact, in real JS world it's quite the opposite:
var obj = {
foo: function bar() { console.log('baz'); }
};
Contains declaration of object
obj. Assignment expression and declaration of function bar
to the propety foo
and also expression of the function bar
itself.
Perhaps a much easier way to comprehend this would be:
var x = 7;
Where we first declare a variable x (var x
) which is hoisted by the compiler to the top of our scope and then we assign it x = 7;
but as a line by itself, it contains both a declaration and an expression