Questions tagged [undefined]

A variable is undefined if it has not been assigned a value.

A variable is undefined if it has not been assigned a value. Depending on the programming language used this might be an actual value or the variable gets assigned some default value like "" (the empty String), 0 or NULL or accessing the undefined variable might result in some kind of error condition.

Use this tag for questions about the behavior of undefined variables or when you see undefined values where you don't expect them.

6824 questions
3201
votes
49 answers

Detecting an undefined object property

How do I check if an object property in JavaScript is undefined?
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
3026
votes
47 answers

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

Is there a universal JavaScript function that checks that a variable has a value and ensures that it's not undefined or null? I've got this code, but I'm not sure if it covers all cases: function isEmpty(val){ return (val === undefined || val ==…
Alex
  • 34,699
  • 13
  • 75
  • 158
3022
votes
16 answers

How can I check for "undefined" in JavaScript?

What is the most appropriate way to test if a variable is undefined in JavaScript? I've seen several possible ways: if (window.myVariable) Or if (typeof(myVariable) != "undefined") Or if (myVariable) // This throws an error if undefined. Should…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
2645
votes
34 answers

How can I determine if a variable is 'undefined' or 'null'?

How do I determine if a variable is undefined or null? My code is as follows: var EmpName = $("div#esd-names div#name").attr('class'); if(EmpName == 'undefined'){ // DO SOMETHING };
But when I…
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
2361
votes
31 answers

JavaScript check if variable exists (is defined/initialized)

Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.)) if (elem) { // or !elem or if (typeof elem !== 'undefined') { or if (elem != null) {
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
1624
votes
39 answers

What is the difference between null and undefined in JavaScript?

I want to know what the difference is between null and undefined in JavaScript.
user605334
1046
votes
15 answers

How to check a not-defined variable in JavaScript

I wanted to check whether the variable is defined or not. For example, the following throws a not-defined error alert( x ); How can I catch this error?
Jineesh
  • 11,187
  • 5
  • 24
  • 25
931
votes
25 answers

Why is null an object and what's the difference between null and undefined?

Why is null considered an object in JavaScript? Is checking if ( object == null ) Do something the same as if ( !object ) Do something ? And also: What is the difference between null and undefined?
rahul
  • 184,426
  • 49
  • 232
  • 263
677
votes
12 answers

How can I unset a JavaScript variable?

I have a global variable in JavaScript (actually a window property, but I don't think it matters) which was already populated by a previous script, but I don't want another script that will run later to see its value or that it was even…
Guss
  • 30,470
  • 17
  • 104
  • 128
646
votes
29 answers

How to check for an undefined or null variable in JavaScript?

We are frequently using the following code pattern in our JavaScript code if (typeof(some_variable) != 'undefined' && some_variable != null) { // Do something with some_variable } Is there a less verbose way of checking that has the same…
Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
614
votes
9 answers

JavaScript checking for null vs. undefined and difference between == and ===

How do I check a variable if it's null or undefined and what is the difference between the null and undefined? What is the difference between == and === (it's hard to search Google for "===" )?
MUG4N
  • 19,377
  • 11
  • 56
  • 83
460
votes
0 answers

How can I check whether a variable is defined in JavaScript?

How to check whether a JavaScript variable defined in cross-browser way? I ran into this problem when writing some JavaScript utilizing FireBug logging. I wrote some code like below: function profileRun(f) { // f: functions to be profiled …
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
368
votes
9 answers

variable === undefined vs. typeof variable === "undefined"

The jQuery Core Style Guidelines suggest two different ways to check whether a variable is defined. Global Variables: typeof variable === "undefined" Local Variables: variable === undefined Properties: object.prop === undefined Why does jQuery use…
Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
361
votes
18 answers

Check if object exists in JavaScript

How do I verify the existence of an object in JavaScript? The following works: if (!null) alert("GOT HERE"); But this throws an Error: if (!maybeObject) alert("GOT HERE"); The Error: maybeObject is not defined.
Yarin
  • 173,523
  • 149
  • 402
  • 512
351
votes
10 answers

Can I set variables to undefined or pass undefined as an argument?

I’m a bit confused about JavaScript’s undefined and null values. What does if (!testvar) actually do? Does it test for undefined and null or just undefined? Once a variable is defined can I clear it back to undefined (therefore deleting the…
AJ.
  • 10,732
  • 13
  • 41
  • 50
1
2 3
99 100