+ 4
What are undeclared and undefined variables in Javascript?
Please explain with appropriate example. Thanks
15 Antworten
+ 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
+ 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);
+ 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.
+ 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.
+ 3
Check this link here best explanation is given
http://www.dotnetfunda.com/interviews/show/4024/difference-between-undefined-and-undeclared-variables
+ 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
+ 3
Ярослав Вернигора (Yaroslav Vernigora) Sorry for my mistake just replace string with var keyword
+ 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"
0
they have not given proper example in this link.Is there any other example .
0
also i have a doubt that is it true in undeclared variable doesnt throw an error because its assigned to global scope.
0
And can there be an example for undefined variable.
0
console.log(x); // x is not declared
String son;
Console.log(son); // son declared but undefined
0
Maruthi M.B "string" from what language?
0
JavaScript