JAVASCRIPT QUESTION 🙋 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 24

JAVASCRIPT QUESTION 🙋

Hi guys! What's the difference between the 2 scenarios ("Undefined" and "a is not defined"): >> Scenario 1: // a is declared but not assigned a value var a; console.log(a); => Undefined >> Scenario 2: // a is not declared at all console.log(a); => UncaughtReferenceError: a is not defined

28th Aug 2020, 12:51 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
14 Answers
+ 26
Good question. In the 1st case you will get the default value of undefined Vs Reference error in case 2 stopping code execution. #check the demo in this code https://code.sololearn.com/WNltnhSWfQ2X/ Also, according to this SO post's top answer : https://stackoverflow.com/questions/24811001/javascript-unassigned-var > undefined is a property of the global object, i.e. it is a variable in global scope. The initial value of undefined is the primitive value undefined. > A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined > if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.
28th Aug 2020, 1:13 PM
Lord Krishna
Lord Krishna - avatar
+ 18
Using the var key word you initialise the variable a and it is always initialised to undefined. If the system doesn't find any variable a in the storage then it shows an error that it cannot find any variable named a. In the scenario 1, the output is not an error In the scenario 2, it shows the error (as told above) That is why it is good to declare the variables using the var keyword at the beginning itself! In code example: if (5 > 6) var a = 10; console.log(a); Output is an error! But, if you had already initialised the variable (before the if block) then it wouldn't have shown an error
28th Aug 2020, 1:14 PM
Namit Jain
Namit Jain - avatar
+ 11
Think of variables like boxes with labels. In first scenario you have created a box, and put a label titled "a" on it. But you haven't stored anything in the box, and so the content of the box is said to be "undefined". In second scenario you force someone (named "console") to open a box labeled "a", and show its content. But there is no such thing as "a" box. You haven't created that box labeled "a" yet. The second scenario is worse than the first, not only the box content is undefined, the box itself does not even exist.
28th Aug 2020, 1:18 PM
Ipang
28th Aug 2020, 7:47 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 6
undefined is an actual value when you do not assign any value to any declared variable. But if you try to access it without definition the error says "abc variable is undefined", here undefined is used as word from english which means not defined(un-defined)
28th Aug 2020, 2:17 PM
Raj Chhatrala
Raj Chhatrala - avatar
29th Aug 2020, 8:33 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 4
In the first case the type of object is not specified in the latter the object itself does not exist 🤔
29th Aug 2020, 3:49 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 3
1) Variable have no value 2) Variable do not exists
29th Aug 2020, 8:09 PM
Maciej Błędkowski
Maciej Błędkowski - avatar
+ 2
Case 1: The value of "a" is undefined (which is a value like "dog" or 2.5 or false) that normally indicates that the programmer didn't give the variable "a" a value or when no return value is given for a function... Case 2: "a" itself is not defined, meaning there exist no word "a" in the javascript dictionary.
29th Aug 2020, 4:51 PM
mohammad zahweh
mohammad zahweh - avatar
+ 2
Hi
29th Aug 2020, 6:16 PM
Mark Pindon
+ 2
Undefined is applied when a variable has not datatype or it's incompatible. This occur cause it exists a function could not return a value or it is not instantiated. For example, when you consume a web service. a is not defined occur when you have not declared, it is almost equivalent with a difference in the context. Scenario 1 indicate you declared your variable a, but you didn't define a datatype, in consequence you have to assign a value, and automatically this one already a datatype. In the case of Scenario 2, you don't declare variable a, for this reason it get it out a not defined.
29th Aug 2020, 9:25 PM
Josué Becerra
Josué Becerra - avatar
+ 1
In the first scenario , we get output as undefined because we did not intialise it. But in second scenario we get an error. In Js First we should declare the varaible and should use it.
30th Aug 2020, 8:46 AM
Kandagatla Devika
Kandagatla Devika - avatar
+ 1
var a; is the same as var a = undefined;
30th Aug 2020, 10:46 AM
Sebastian Ahlborn
Sebastian Ahlborn - avatar
0
Var do not exists
30th Aug 2020, 12:51 PM
Isuru Umayanga
Isuru Umayanga - avatar