Undefined values: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: removed first and last blank lines. -- ~~~~)
Line 483: Line 483:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
In Javascript undefined is a property of the global object, i.e. it is a variable in global scope. The initial value of undefined is the primitive value undefined. The problem with using undefined is that undefined is mutable. Instead we can use typeof to check if a value is undefined.
<lang javascript>var a;
<lang javascript>var a;


typeof(a) == "undefined";
typeof(a) === "undefined";
typeof(b) == "undefined";
typeof(b) === "undefined";


var obj = {}; // Empty object.
var obj = {}; // Empty object.
obj.c == null;
typeof(obj.c) === "undefined";


obj.c = 42;
obj.c = 42;


obj.c == 42;
obj.c === 42;
delete obj.c;
delete obj.c;
obj.c == null;</lang>
typeof(obj.c) === "undefined";</lang>

We can also use the prefix keyword void, it always returns undefined. But this will throw a error if the variable has not been defined.
<lang javascript>var a;
a === void 0; // true
b === void 0; // throws a ReferenceError</lang>


=={{header|Logo}}==
=={{header|Logo}}==