0

It may seem very trivial issue but very confusing and recurring for me. In some manuals for javascript or tutorials these terms are used alternately. In others I found the explanation that we declare variables when we create them with var const let and we define variables, when we append some value/object to the declared variable as below:

var name; //declaring
name = 'Adam' //defining
var age = 'dead' //declaring + defining

Are there any approved and correct rules of using these two terms?

8
  • 1
    The first line technically does define the variable as undefined, which is a valid primitive data type in JS. The second line actually re-defines the variable to the string 'Adam'. Commented Aug 31, 2017 at 3:20
  • @skyline3000 right! Commented Aug 31, 2017 at 3:23
  • note, there is an important difference between var and let Commented Aug 31, 2017 at 3:23
  • You might want to look up the term variable hoisting. Commented Aug 31, 2017 at 3:24
  • 1
    Technical details between programming languages aside, I don't think any programmer would be confused by the terminology you're using - it's pretty much the standard in all modern languages. As I mentioned in the first comment though, just note the small nuance with undefined in JS since not all languages have that concept. Commented Aug 31, 2017 at 3:29

2 Answers 2

3

I'd say that "variable definition" is not a standard JavaScript term.

Functions (of all kinds) and object properties can get defined, but variables always get declared. This terminology might hint at the declarative nature of variables - a declaration always applies to the complete current scope, it's not an action that gets executed and does something.

var name is a declaration. var age = 'dead' is a declaration with an initialiser. name = 'Adam' is just an assignment. I'd guess that "defining" a variable refers to it no longer being undefined, so both an assignment statement or the initialiser of the declaration might do that. I'd rather speak of the initialisation of the variable, though.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.