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
266
votes
13 answers

Set a variable if undefined in JavaScript

I know that I can test for a JavaScript variable and then define it if it is undefined, but is there not some way of saying var setVariable = localStorage.getItem('value') || 0; seems like a much clearer way, and I'm pretty sure I've seen this in…
pedalpete
  • 21,076
  • 45
  • 128
  • 239
259
votes
1 answer

How to check if a JavaScript variable is NOT undefined?

Things I’ve tried that don’t seem to work: if(lastName != "undefined") if(lastName != undefined) if(undefined != lastName)
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
238
votes
14 answers

"undefined" function declared in another file?

I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns: undefined: NewEmployee Here is the source code: main.go: package main func main() { emp := NewEmployee() …
Juan M
  • 4,063
  • 4
  • 19
  • 28
224
votes
11 answers

Test if something is not undefined in JavaScript

I'm checking if(response[0].title !== undefined), but I get the error: Uncaught TypeError: Cannot read property 'title' of undefined.
Raimonds
  • 2,606
  • 3
  • 20
  • 25
200
votes
11 answers

What are all the common undefined behaviours that a C++ programmer should know about?

What are all the common undefined behaviours that a C++ programmer should know about? Say, like: a[i] = i++;
yesraaj
  • 46,370
  • 69
  • 194
  • 251
197
votes
2 answers

What does the PHP error message "Notice: Use of undefined constant" mean?

PHP is writing this error in the logs: "Notice: Use of undefined constant". Error in logs: PHP Notice: Use of undefined constant department - assumed 'department' (line 5) PHP Notice: Use of undefined constant name - assumed 'name' (line 6) PHP…
Nik
  • 2,424
  • 3
  • 18
  • 16
195
votes
10 answers

How to check 'undefined' value in jQuery

Possible Duplicate: Detecting an undefined object property in JavaScript javascript undefined compare How we can add a check for an undefined variable, like: function A(val) { if (val == undefined) // do this else // do this }
Gaurav
  • 8,367
  • 14
  • 55
  • 90
190
votes
9 answers

Javascript - removing undefined fields from an object

Is there a clean way to remove undefined fields from an object? i.e. > var obj = { a: 1, b: undefined, c: 3 } > removeUndefined(obj) { a: 1, c: 3 } I came across two solutions: _.each(query, function removeUndefined(value, key) { if…
Damian
  • 2,223
  • 3
  • 16
  • 12
185
votes
8 answers

JavaScript null check

I've come across the following code: function test(data) { if (data != null && data !== undefined) { // some code here } } I'm somewhat new to JavaScript, but, from other questions I've been reading here, I'm under the impression…
afsantos
  • 5,178
  • 4
  • 30
  • 54
178
votes
19 answers

react evironment variables .env return undefined

I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined. import React, {…
David whyte
  • 1,801
  • 2
  • 8
  • 6
177
votes
14 answers

How to resolve TypeError: Cannot convert undefined or null to object

I've written a couple of functions that effectively replicate JSON.stringify(), converting a range of values into stringified versions. When I port my code over to JSBin and run it on some sample values, it functions just fine. But I'm getting this…
zahabba
  • 2,921
  • 5
  • 20
  • 33
165
votes
20 answers

What reason is there to use null instead of undefined in JavaScript?

I've been writing JavaScript for quite a long time now, and I have never had a reason to use null. It seems that undefined is always preferable and serves the same purpose programmatically. What are some practical reasons to use null instead of…
Jimmy
  • 35,686
  • 13
  • 80
  • 98
162
votes
10 answers

Is it better to return `undefined` or `null` from a javascript function?

I have a function which I have written which basically looks like this: function getNextCard(searchTerms) { // Setup Some Variables // Do a bunch of logic to pick the next card based on termed passed through what I'll call here as 'searchTerms'…
Jeremy Iglehart
  • 4,281
  • 5
  • 25
  • 38
128
votes
7 answers

remove null or undefined from properties of a type

I need to declare a type such that removes the undefined from its property types. Suppose we have: type Type1{ prop?: number; } type Type2{ prop: number | undefined; } type Type3{ prop: number; } I need to define a generic type called…
Fartab
  • 4,725
  • 2
  • 26
  • 39
128
votes
12 answers

Why is there a `null` value in JavaScript?

In JavaScript, there are two values which basically say 'I don't exist' - undefined and null. A property to which a programmer has not assigned anything will be undefined, but in order for a property to become null, null must be explicitly assigned…
Christoph
  • 164,997
  • 36
  • 182
  • 240