+ 4

What are undeclared and undefined variables in Javascript?

Please explain with appropriate example. Thanks

30th Nov 2019, 12:25 PM
swali1
15 Réponses
+ 8
In JavaScript When you try to use/access/initialize a variable that is never defined before you will get a undefined variable error, For example : If you try - console.log(a); //Here you will get error that is undefined variable. But if you have a variable defined but not initialized, then you try to use/access Like var a; console.log(a); Here variable "a" value is undefined -> difference between undeclared and undefined it, in undeclared variable you get console error but in undefined you just get value as undefined
30th Nov 2019, 12:53 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 6
Try to run this, you will understand exact difference //Here value of a is undefined as it is not initialized var a; console.log(a); //Here variable b is undefined as well but this will throw error as it can not fine variable at all console.log(b);
30th Nov 2019, 1:00 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 6
Simply undeclared is a variable that is not declared and undefined is one that is declared without any value or not declared Forexample: var x; console.log(x) output:undefined Because the value of x is not defined console.log(y); output:undefined Because y is not declared or defined You can say every undeclared variable is undefined but every undefined variable is not undeclared it is possible to declare variable and still it remains undefined, untill you give it some value.
30th Nov 2019, 6:48 PM
Hafsa Mohamed
Hafsa Mohamed - avatar
+ 4
swali1 yes, if you initialize a variable without declaring, it will be added to global scope as a property and behaves like a global variable after that initialization. https://code.sololearn.com/WO24lIawxM1V/?ref=app it is not a good practice and can cause some hard-to-find bug.
30th Nov 2019, 1:43 PM
Gordon
Gordon - avatar
30th Nov 2019, 12:28 PM
A͢J
A͢J - avatar
+ 3
swali1 Because you didn't understand the theory part. It's simple See this var a; // not initialised When you print it give result undefined as it is not initialised with any value like a = 1; But it will not give error. It gives undefined value. And for undeclared variable:- if you print any variable without declared it you will get error as variable is not defined means undeclared because it doesn't declared anywhere and you are using it. console.log(b)// error will come as b is not defined. Check this code to understand. https://code.sololearn.com/WL95AoS7qBvY/?ref=app
30th Nov 2019, 1:52 PM
A͢J
A͢J - avatar
+ 3
Ярослав Вернигора (Yaroslav Vernigora) Sorry for my mistake just replace string with var keyword
2nd Dec 2019, 2:14 AM
onekpsc
+ 2
imagine the variable as an empty box. and your program in the form of a room. while the box is outside the room its for the program does not exist it does not see it. this is called an "undeclared" variable. when we declare a variable (give it a name and write the keyword var, const or let before the name), we bring the box into the room. now the program sees the variable and can refer to it by name. but the box is still empty. there is no meaning in it. if we give the command to output a value from this variable we get the value "undefined"
30th Nov 2019, 3:12 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
they have not given proper example in this link.Is there any other example .
30th Nov 2019, 12:41 PM
swali1
0
also i have a doubt that is it true in undeclared variable doesnt throw an error because its assigned to global scope.
30th Nov 2019, 12:56 PM
swali1
0
And can there be an example for undefined variable.
30th Nov 2019, 12:57 PM
swali1
0
console.log(x); // x is not declared String son; Console.log(son); // son declared but undefined
1st Dec 2019, 5:27 PM
onekpsc
0
Maruthi M.B "string" from what language?
1st Dec 2019, 5:48 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
JavaScript
7th Dec 2019, 7:29 PM
Сергей Мороз
Сергей Мороз - avatar