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
121
votes
6 answers

When is null or undefined used in JavaScript?

I am confused as to when JavaScript returns null or undefined. Also different browsers seem to be returning these differently. I am seeking some examples of null/undefined with the browsers that return them. While I am now clear on the undefined…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
116
votes
5 answers

How to compare variables to undefined, if I don’t know whether they exist?

In JavaScript you can declare a variable and if it’s undefined, you can check variable == undefined; I know that, but how can you compare a value that you don’t know yet if it’s in memory? For example, I have a class which is created when the user…
ncubica
  • 8,169
  • 9
  • 54
  • 72
102
votes
7 answers

JavaScript String concatenation behavior with null or undefined values

As you may know, in JavaScript '' + null = "null" and '' + undefined = "undefined" (in most browsers I can test: Firefox, Chrome and IE). I would like to know the origin of this oddity (what the heck was in the head on Brendan Eich?!) and if there…
Doc Davluz
  • 4,154
  • 5
  • 30
  • 32
100
votes
5 answers

Should JSON include null values

I'm creating an API that returns results as JSON. Is there a current best practice for whether we should include keys in the result when the value is null? For example: { "title":"Foo Bar", "author":"Joe Blow", "isbn":null } or { …
jjathman
  • 12,536
  • 8
  • 29
  • 33
97
votes
8 answers

How dangerous is it in JavaScript, really, to assume undefined is not overwritten?

Every time anyone mentions testing against undefined, it's pointed out that undefined is not a keyword so it could be set to "hello", so you should use typeof x == "undefined" instead. This seems ridiculous to me. Nobody would ever do that, and if…
Cosmologicon
  • 2,127
  • 1
  • 16
  • 18
97
votes
3 answers

Why does TypeScript have both `void` and `undefined`?

In TypeScript, you can annotate a function as returning void: function fn1(): void { // OK } function fn2(): void { // Error return 3; } You can also annotate a function to return undefined: function fn3(): undefined { // OK …
Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
96
votes
4 answers

C++ error 'Undefined reference to Class::Function()'

I was wondering if anyone could help me out with this - I'm only new to C++ and it's causing me a fair amount of troubles. I'm trying to make relatively simple Deck and Card class objects. The error is showing up in "Deck.cpp", declaration of an…
Ben Harris
  • 1,734
  • 3
  • 15
  • 24
95
votes
2 answers

JSLint: was used before it was defined

Hi I have the 3 javascript files. jquery.js utility.js file1.js In file1.js I have jQuery.noConflict() jQuery(document).ready(function($) { // .... }); I get an error 'jQuery' was used before it was defined. and 'document' was used before it…
Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
89
votes
8 answers

CoffeeScript Undefined

In javascript to check if a variable was never created, we just do if (typeof MyVariable !== "undefined"){ ... } I was wonder how I do that in coffeescript?... I try something like if (MyVariable?false){ ... } but this check if MyVariable is a…
Jaider
  • 14,268
  • 5
  • 75
  • 82
85
votes
9 answers

PHP Notice: Undefined offset: 1 with array when reading data

I am getting this PHP error: PHP Notice: Undefined offset: 1 Here is the PHP code that throws it: $file_handle = fopen($path."/Summary/data.txt","r"); //open text file $data = array(); // create new array map while (!feof($file_handle) ) { …
alchuang
  • 3,487
  • 3
  • 34
  • 48
80
votes
9 answers

next.js environment variables are undefined (Next.js 10.0.5)

I am coding a website with Next.js and I tried to add google Tag Manager. I followed the tutorial on the Next.js Github example but for some reasons I can't access to my environment variables. It says my variable is undefined. I created a file…
Florie Anstett
  • 1,135
  • 1
  • 7
  • 15
80
votes
6 answers

Undefined or null for AngularJS

When I write watch handling functions I check newVal param on undefined and null. Why does AngularJS have such a behavior, but doesn't have particular utility method? So there is angular.isUndefined but not angular.isUndefinedOrNull. It isn't hard…
slo2ols
  • 1,009
  • 1
  • 8
  • 16
77
votes
2 answers

Can an optional parameter be null in TypeScript?

According to this article, when strict null checking is enabled in TypeScript, you cannot assign null or undefined to a variable unless it is explicitly allowed with a union. // required value let req: string; req = "Something"; // OK req = null; …
Snackoverflow
  • 5,332
  • 7
  • 39
  • 69
76
votes
4 answers

undefined method `devise_for' in rails

After I install devise and create a user model. I rake db:migrate and then I rake routes. I then get a error with "undefined method `devise_for' for #". What could be causing this error?
Yeddie
  • 863
  • 2
  • 7
  • 7
69
votes
2 answers

undefined reference when calling inline function

I am getting a really odd error from GCC 4.8.1 with inline functions. I have two near-identical inline functions defined in header files (debug.h and error.h) in src/include/, with the only difference being what they print - one prefixes DEBUG: to…
Marco Scannadinari
  • 1,774
  • 2
  • 15
  • 24