undefined vs not defined in JS πŸ€” | Namaste JavaScript Ep. 6

Akshay Saini
23 Oct 202011:01

Summary

TLDRThis JavaScript tutorial delves into the concept of 'undefined', a unique keyword in JavaScript that signifies a variable has been declared but not yet assigned a value. The script explains how JavaScript's execution context pre-allocates memory for variables, even before code execution, setting them to 'undefined' as a placeholder. It contrasts 'undefined' with 'not defined', emphasizing the importance of not manually setting variables to 'undefined'. The video also touches on JavaScript's flexibility as a loosely typed language, allowing variables to hold different data types, and warns against assigning 'undefined' to variables, suggesting it should be reserved for its original purpose of indicating uninitialized variables.

Takeaways

  • 🌐 JavaScript's 'undefined' is a unique keyword not found in other languages, and it plays a significant role in how JavaScript manages memory and execution.
  • πŸ’Ύ Before any code is executed, JavaScript allocates memory for variables and functions, assigning them a 'undefined' placeholder to indicate no value has been set yet.
  • πŸ” Using the debugger, one can observe that variables hold 'undefined' before any values are assigned to them, showcasing JavaScript's pre-allocation of memory.
  • 🚫 Attempting to access a variable that has not been declared results in 'not defined', which is different from 'undefined', indicating no memory has been allocated for it.
  • πŸ”„ JavaScript is a loosely typed language, allowing variables to hold any data type, which contrasts with strictly typed languages that enforce data types for variables.
  • πŸ› οΈ JavaScript performs type coercion behind the scenes, allowing flexibility in assigning different data types to the same variable without explicit type declarations.
  • ❌ It's considered bad practice to assign 'undefined' to a variable as it can lead to inconsistencies and confusion, as 'undefined' is meant to indicate a lack of assignment.
  • πŸ“š Understanding the difference between 'undefined' and 'not defined' is crucial for debugging and writing clear JavaScript code.
  • πŸ”‘ The script emphasizes the importance of proper variable initialization and the avoidance of assigning 'undefined' to variables unnecessarily.
  • 🎯 The video concludes with a teaser for the next topic, 'scope chain', which is a critical concept in JavaScript and a common interview question.

Q & A

  • What is the significance of the 'undefined' keyword in JavaScript?

    -In JavaScript, 'undefined' is a special keyword that signifies a variable has been declared but not yet assigned a value. It is a placeholder value that the JavaScript engine assigns to variables during the memory allocation phase before any code is executed.

  • How does JavaScript's memory allocation for variables work?

    -JavaScript allocates memory to variables during the creation of the global execution context, even before any code is executed. This allocation includes assigning the 'undefined' value as a placeholder until the variable is explicitly assigned a value.

  • What is the difference between 'undefined' and 'not defined' in JavaScript?

    -In JavaScript, 'undefined' refers to a declared variable that has not been assigned a value yet, whereas 'not defined' means the variable has not been declared at all. 'Undefined' takes up memory space with a placeholder value, while 'not defined' variables do not exist in the scope.

  • Why is it considered a bad practice to assign 'undefined' to a variable?

    -Assigning 'undefined' to a variable is considered bad practice because 'undefined' is a special value used by the JavaScript engine to indicate that a variable has not been assigned a value. Overriding this with an assignment can lead to confusion and inconsistencies in the code.

  • What does it mean for JavaScript to be a loosely typed language?

    -Being a loosely typed language means that JavaScript does not require variables to be explicitly typed. A single variable can hold values of different types throughout its lifecycle without needing a type declaration.

  • Can you provide an example of how JavaScript handles type coercion?

    -JavaScript handles type coercion automatically when different data types are used in expressions. For example, if a string and a number are used in an arithmetic operation, JavaScript will convert the string to a number to perform the calculation.

  • What is the purpose of using 'debugger' in JavaScript?

    -The 'debugger' statement in JavaScript is used to initiate a debugging session at that point in the code. When the code execution reaches this statement, it pauses, allowing developers to inspect variables and step through the code to debug issues.

  • How can you check if a variable is 'undefined' in JavaScript?

    -You can check if a variable is 'undefined' in JavaScript using the strict equality operator '==='. For example, 'if (a === undefined)' will evaluate to true if the variable 'a' has not been assigned a value.

  • What is the scope chain, and why is it important in JavaScript?

    -The scope chain is a data structure in JavaScript that keeps track of all the variables that are currently in the scope of the currently executing code. It is important for understanding how variables are looked up and accessed in different scopes, which is crucial for writing maintainable and efficient code.

  • Why is the scope chain a hot interview topic for JavaScript developers?

    -The scope chain is a hot interview topic because it tests a candidate's understanding of how JavaScript manages variable scope and execution contexts. Mastery of scope chain concepts is essential for writing efficient and bug-free JavaScript code.

Outlines

00:00

πŸ’‘ Understanding 'undefined' in JavaScript

This paragraph explains the concept of 'undefined' in JavaScript, which is a unique keyword not found in other programming languages. It highlights how JavaScript allocates memory for variables and functions before executing any code, resulting in variables initially holding the 'undefined' value. The paragraph uses the debugger tool to illustrate that memory is reserved for a variable 'a' before it is assigned a value of 7. It emphasizes the difference between 'undefined' and 'not defined', where 'undefined' is a placeholder in memory indicating no value has been assigned yet, whereas 'not defined' means no memory has been allocated. The paragraph also clarifies that 'undefined' is not equivalent to an empty value or no memory allocation.

05:00

πŸ”‘ JavaScript's Loose Typing and Variable Flexibility

The second paragraph delves into JavaScript's loosely typed nature, allowing variables to hold different data types without being restricted to a specific one. It demonstrates how a variable can successively hold a string, a number, and then revert to a string, showcasing JavaScript's flexibility. The speaker advises against deliberately assigning the 'undefined' value to a variable, as it is meant to serve as a placeholder indicating that no value has been assigned. The paragraph concludes with a caution against misusing 'undefined' and encourages letting it serve its intended purpose as an indicator of unassigned variables.

10:01

πŸ“š Upcoming Topic: JavaScript Scope Chain

The final paragraph briefly introduces the upcoming topic of the scope chain in JavaScript, hinting at its importance and relevance in interviews. It encourages viewers to watch the next video for a deeper understanding of this concept. The speaker also requests viewers to like the video and thanks them for watching, signing off with a 'namaste'.

Mindmap

Keywords

πŸ’‘undefined

In JavaScript, 'undefined' is a special keyword that represents the absence of a value. It is used to indicate that a variable has been declared but not yet assigned a value. In the script, the speaker explains that JavaScript allocates memory for variables before any code is executed, and during this allocation, variables are initialized to 'undefined'. This is different from 'not defined', where no memory has been allocated for the variable. The script uses the debugger to illustrate that 'a' is 'undefined' before the line 'a = 7' is executed.

πŸ’‘global execution context

The 'global execution context' refers to the environment in which JavaScript code is executed. It is a phase during which the JavaScript engine sets up the global scope, hoists declarations, and initializes built-in objects. The script mentions that JavaScript creates a global execution context and allocates memory to all variables and functions before executing any code, which is when 'undefined' comes into play.

πŸ’‘memory allocation

Memory allocation in the context of the script refers to the process by which JavaScript reserves space in memory for variables and functions. The speaker uses the debugger to demonstrate that memory is allocated to variable 'a' before it is assigned a value, resulting in it being 'undefined'. This allocation is a fundamental aspect of how JavaScript manages variables.

πŸ’‘loose typing

Loose typing, also known as weak typing, means that a programming language does not enforce strict data types for variables. In JavaScript, this allows a single variable to hold different data types throughout its lifecycle. The script explains that JavaScript is a loosely typed language, which is why a variable like 'a' can hold 'undefined', a number, or a string, depending on how it is assigned.

πŸ’‘hoisting

Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase. The script touches on hoisting when discussing how memory is allocated to variables before any code is executed, which is part of the JavaScript engine's preparation of the global execution context.

πŸ’‘not defined

'Not defined' in JavaScript refers to a variable that has not been declared at all, hence no memory has been allocated for it. This is contrasted with 'undefined', which is a value that indicates a variable has been declared but not yet assigned. The script uses the example of trying to access a variable 'x' that has not been declared to illustrate 'not defined'.

πŸ’‘debugger

The 'debugger' statement in JavaScript is used to initiate a debugging session at a specified point in the code. In the script, the speaker uses 'debugger' to pause execution before the line 'a = 7' is executed, allowing the demonstration of how variables are 'undefined' before they are assigned a value.

πŸ’‘strict equality

Strict equality (===) is an operator in JavaScript that checks if two values are equal and of the same type. The script uses '===' to check if a variable 'a' is strictly equal to 'undefined', which is a way to explicitly test for the 'undefined' value.

πŸ’‘variable assignment

Variable assignment in JavaScript involves assigning a value to a variable. The script explains that once a variable like 'a' is assigned a value (e.g., 'a = 7'), the 'undefined' value is replaced with the assigned value. This highlights how variables can change their values over time in JavaScript.

πŸ’‘scope chain

Although not directly explained in the script, the scope chain is a concept that is mentioned as a topic for a future video. The scope chain is a list of scopes that is searched when looking up variables and functions in JavaScript. It is related to how variables are accessed and managed within different scopes (global, local, block).

Highlights

Undefined is a unique keyword in JavaScript, signifying a variable has been declared but not yet assigned a value.

JavaScript's execution context creates memory allocation for variables and functions before any code is executed.

The debugger can be used to inspect the memory allocation and the 'undefined' state of a variable before it's assigned a value.

Variables in JavaScript are initialized with 'undefined' as a placeholder before they are assigned an actual value.

Accessing a variable that has not been declared results in a 'not defined' error, as opposed to 'undefined' which indicates declared but not assigned.

The difference between 'undefined' and 'not defined' is clarified, with 'undefined' indicating memory allocation and 'not defined' indicating no memory allocation.

JavaScript is a loosely typed language, allowing variables to hold different data types throughout their lifecycle.

Loosely typed languages provide flexibility but also require careful handling of data types to avoid unexpected behavior.

It's demonstrated that a variable can hold different types, such as numbers and strings, at different points in the code.

Assigning 'undefined' to a variable is discouraged as it can lead to inconsistencies and is not a recommended practice.

The 'undefined' keyword should be reserved for its original purpose of indicating whether a variable has been assigned a value.

The video concludes with a teaser for the next topic, the scope chain, which is an important concept and a common interview question.

The presenter encourages viewers to give the video a thumbs up and watch the next video on the scope chain.

The video ends with a musical note, signaling the conclusion and a friendly farewell.

Transcripts

play00:00

undefined is a very special keyword in

play00:02

javascript

play00:03

and it is not there in other languages

play00:05

it has a lot to do with

play00:07

how javascript code is executed so as we

play00:10

have studied in the previous videos also

play00:12

that javascript code is executed in a

play00:15

different way

play00:16

it creates a global execution context

play00:18

and allocates memory to

play00:20

all the variables and functions even

play00:22

before a single line of code is executed

play00:24

remember this so undefined comes into

play00:27

picture

play00:28

here only let us see with the code also

play00:31

so suppose we created a variable

play00:33

a is equals to 7 right so even before

play00:36

this line of code is executed

play00:38

right javascript has tried to allocate

play00:41

memory to this

play00:42

a right memory space to a even before

play00:45

this line of code is run

play00:47

okay let me show you with the help of

play00:49

debugger so i am putting the debugger

play00:51

right before this line is executed okay

play00:55

so let us see an interesting thing so if

play00:58

we refresh the page

play00:59

this a this line hasn't been executed

play01:03

yet

play01:03

right but javascript has already

play01:06

allocated memory to this

play01:08

a okay so we have already reserved

play01:10

memory for a now

play01:12

okay so if we see here is the reserved

play01:15

memory

play01:16

so right now a is undefined so at this

play01:19

point if you try to

play01:20

in the console if you try to log what a

play01:23

is see i have hold the program

play01:25

i have held the program like i put a

play01:26

debugger right so i have hold the

play01:28

program at that state itself

play01:30

so if you try to log a right now it is

play01:32

undefined

play01:33

so why this happened okay so when it

play01:36

allocates memory to all the variables

play01:38

and functions

play01:40

to the variables it tries to put a

play01:43

placeholder right it is like a

play01:46

placeholder which is

play01:47

placed in the memory okay that special

play01:50

keyword is

play01:51

undefined so if we try to lock this

play01:54

so you get undefined in it so undefined

play01:57

is

play01:57

like taking memory it is it is very

play02:00

different than not defined okay

play02:01

so suppose if i try to access something

play02:04

else right so suppose if i

play02:06

try to do a console log of x over here

play02:09

okay

play02:09

so which we have not allocated memory to

play02:12

okay if we run this code if we run this

play02:15

code we have not allocated any memory to

play02:17

x right

play02:18

so what will happen if we try to find

play02:21

x you won't find anything that is known

play02:24

as

play02:24

not defined but we have allocated memory

play02:28

to

play02:29

a if you try to console log a

play02:32

we can find that so even before i put

play02:35

this where

play02:36

a is equals to 7 right so suppose if i

play02:38

try to log a

play02:39

over here even before putting 7 to a

play02:43

i am trying to log this a so what

play02:45

happens is

play02:46

what happens is let me just run

play02:50

it so in the console you will find that

play02:53

we have printed undefined this a was

play02:56

printed undefined

play02:57

this is the value with javascript engine

play02:59

allocated to a

play03:01

while creating the memory okay but once

play03:03

this line has been executed

play03:05

after this line you will see that the

play03:08

value of a has changed to

play03:10

7 okay let me just remove that x if you

play03:13

try to log here see it was undefined

play03:15

before

play03:16

this undefined was coming from the place

play03:20

just because javascript engine allocated

play03:23

undefined that special placeholder

play03:26

to a okay not like not defined

play03:29

not defined in something which has not

play03:31

been allocated memory

play03:32

okay so this is the difference between

play03:35

undefined

play03:36

and not defined okay we'll see something

play03:38

more

play03:39

so remember undefined is not equal to

play03:42

empty some people think that

play03:43

undefined means empty like it is not

play03:45

taking up any memory space or something

play03:47

no it is a special keyword it takes up

play03:49

its own memory

play03:50

but you can assume it to be like a

play03:52

placeholder which is kept for the time

play03:54

being

play03:55

until the variable is assigned some

play03:57

other value okay

play03:58

till that point it will store this

play04:01

placeholder known as undefined

play04:03

okay and one more thing i would show you

play04:05

right now

play04:06

is suppose if i do um suppose this is

play04:09

undefined right

play04:10

so if i never put any value inside a

play04:13

right like i just declared that okay

play04:15

where a

play04:16

and i never put any value to it so it

play04:19

will throughout our whole program

play04:21

have this value undefined that

play04:23

placeholder kept inside a

play04:25

and to show you that it is something in

play04:28

javascript

play04:29

we can also do something like this okay

play04:31

uh so suppose

play04:32

if i have my uh if

play04:36

a is equal equal equal to undefined so

play04:39

equal equal equal to you might not be

play04:40

aware of the syntax

play04:42

but see it is just checking whether two

play04:44

things are equal or not

play04:45

and strictly checking we will talk about

play04:47

it later in the videos okay that will be

play04:49

interesting

play04:50

but for now just assume that it is

play04:51

checking two things are equal or not

play04:53

so if we try to see that a is equal

play04:56

equal to undefined

play04:57

okay that undefined is a thing or not so

play05:00

if we try to do a console log of

play05:03

um over here let us just log it

play05:06

a is undefined okay

play05:09

and if we try to do something like

play05:12

else we just console log

play05:18

a um string a is

play05:21

not undefined okay so if we try to

play05:25

check like this what you will see see

play05:28

a is undefined okay a is undefined it is

play05:31

like

play05:32

it goes into this if it meets this

play05:35

condition

play05:35

it go inside this if block and suppose

play05:38

if i put some value in a

play05:41

okay a is equals to 10 meanwhile so it

play05:43

will say

play05:44

that a is not undefined it has some

play05:47

value in it okay

play05:48

now the a is replaced that undefined is

play05:51

replaced by

play05:52

10. interesting right now when we are

play05:55

talking about

play05:55

undefined let me tell you a little bit

play05:57

more about javascript and variables in

play05:59

it okay

play06:00

so you know javascript is a loosely

play06:03

typed language

play06:04

so loosely typed means that it does not

play06:06

attaches

play06:07

its variables to any specific data type

play06:10

so suppose if i created

play06:12

a and put in string in it so later on in

play06:15

the program i can also put numbers in it

play06:17

i can also put boolean in it

play06:19

it is like very javascript is very

play06:21

flexible in this case

play06:23

right it is loosely typed that is known

play06:25

as loosely type language

play06:26

if it was strict type then just like

play06:29

other languages just like c

play06:31

or c plus plus if you assign a variable

play06:34

that

play06:34

if it is a string a so it will only hold

play06:37

string

play06:38

it won't hold numbers or booleans or

play06:41

anything else

play06:42

but it is not the case in javascript you

play06:44

can put anything

play06:45

and everything inside this variable okay

play06:48

so

play06:48

if i do a var a for the time being

play06:52

it can like just hold undefined

play06:55

okay if i do a console log over here it

play06:58

will just hold

play06:59

undefined okay and if i later on put a

play07:02

is equals to 10 here

play07:03

so and try to log it again so it will it

play07:06

can hold numbers also

play07:08

and it can also hold a string again

play07:11

okay so this is a perfectly valid

play07:13

javascript code okay

play07:15

um i mistyped it

play07:18

no issues so it is a perfectly fine

play07:20

javascript code

play07:21

okay and if i log this a once again

play07:24

okay so see what the output will be

play07:27

undefined

play07:28

so until unless any value is specified

play07:30

there is a placeholder undefined

play07:32

when you have put in 10 so it is 10 now

play07:35

and when you put in string hello world

play07:38

it is hello world now

play07:39

ok so javascript is very flexible in

play07:41

this case

play07:42

that is why javascript is known as

play07:44

loosely type language

play07:46

loosely typed language are also known as

play07:48

weekly typed language

play07:49

weekly type language so it does not mean

play07:52

that it is weak in any sense

play07:54

it does not have any deficiency or

play07:56

something it is not weak at all

play07:58

in fact i feel that the language is more

play08:00

stronger

play08:01

okay because it is handling a lot of

play08:04

stuff behind the scenes okay you give it

play08:07

string it handles automatically behind

play08:09

the scenes it okay it's a string

play08:11

you give it a number it takes it and

play08:13

puts number in it and manages all these

play08:15

things behind it so it is not weak at

play08:17

all for sure

play08:18

in fact i feel that it is more flexible

play08:21

and beautiful

play08:21

in this case okay so and it has to do a

play08:25

lot of things

play08:26

while coercion also okay when you change

play08:29

one type to another type

play08:30

it is doing a lot of stuff we might

play08:32

cover that in the later videos

play08:34

for now just understand that javascript

play08:37

is a loosely typed language

play08:38

and some people also call it as a weekly

play08:40

type language okay

play08:42

so let me tell you an important thing

play08:44

never do this mistake okay

play08:45

never do this mistake of doing something

play08:49

like this

play08:50

a is equal to undefined what does that

play08:52

mean

play08:53

see undefined is a value it is a keyword

play08:55

in javascript

play08:57

and it is totally possible to assign and

play09:00

define to any variable

play09:02

but it is kind of a mistake and it is

play09:05

a bad thing to do in javascript okay you

play09:07

should not do it

play09:08

because see first of all what is

play09:10

undefined so undefined is like a

play09:13

placeholder which is

play09:14

kept inside the variables

play09:18

and it states that in the whole code

play09:21

that variable

play09:22

was not assigned anything right so it is

play09:25

meant for a specific purpose

play09:27

so it is not generally a good way to put

play09:29

undefined like this

play09:31

though it is a totally okay to do this

play09:33

okay if you just

play09:34

log it once again if you log a once

play09:36

again you will see that okay you

play09:38

you can put undefined once again but it

play09:40

can lead to a lot of inconsistencies

play09:42

so it is not a good practice to do this

play09:45

i won't say it's a mistake

play09:46

but it's not a good practice or a

play09:49

healthy practice to

play09:50

put undefined into any variable so don't

play09:52

do this mistake right

play09:54

let us just keep undefined for its own

play09:56

purpose

play09:57

right undefined was meant to uh

play10:00

know okay that whether that variable

play10:04

was assigned anything or not okay so let

play10:06

it be

play10:07

used for that space only and for the js

play10:10

community

play10:11

let us not ever put undefined into any

play10:13

variable

play10:14

okay so it's just a request that's all

play10:17

for

play10:18

now in the next video we'll be covering

play10:19

the scope chain it is a very beautiful

play10:22

topic i would say

play10:23

and it is very hot interview topic also

play10:25

okay in the interview

play10:26

a lot of people ask about what is a

play10:28

scope chain so definitely watch that

play10:30

video

play10:31

so before moving on to that video just

play10:33

like always

play10:34

give this video a thumbs up and thank

play10:36

you for watching namaste javascript

play10:47

[Music]

play11:00

you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
JavaScriptUndefinedVariable AllocationDebuggingLoosely TypedProgrammingWeb DevelopmentCode ExecutionMemory ManagementScope Chain