What’s the difference between variable definition and declaration in JavaScript?

var x is a declaration because you are not defining what value it holds but you are declaring its existence and the need for memory allocation.

var x = 1 is both declaration and definition but are separated with x being declared in the beginning while its definition comes at the line specified (variable assignments happen inline).

I see that you already understand the concept of hoisting but for those that don’t, Javascript takes every variable and function declaration and brings it to the top (of its corresponding scope) then trickles down assigning them in order.

You seem to know most of this already though. Here’s a great resource if you want some advanced, in-depth exploration. Yet I have a feeling you’ve been there before.

Javascript Garden

PS – your analogy between C variable dec/def and JS was spot on. What you read on Wikipedia was correct.

Leave a Comment