0

I am asking this question after reading this question.

NOTE I know the difference between null and undefined.

Let me show you an example,

let a = {};

console.log(a.test); // undefined

Now what if I add a property with test1: undefined

a.test1 = undefined;

console.log(test1); // undefined
console.log(a); // { test1: undefined }

Now tell me what is undefined. Is it a value or something

tbhaxor
  • 1,659
  • 2
  • 13
  • 43
  • *"`undefined` is a property of the global object. That is, it is a variable in global scope. The initial value of `undefined` is the primitive value `undefined`."* - From [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) – Tyler Roper Jan 29 '20 at 04:23
  • 1
    Does this answer your question? [What is the difference between null and undefined in JavaScript?](https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript) –  Jan 29 '20 at 04:23

2 Answers2

3

It is a one of the javascript primitive types.

Javascript will assign undefined to any variable or object's property which is not declared.

undefined is one of the falsy values in javascript like null.

let a = {};

console.log(a.test); //  undefined

In the above snippet test property is undefined cause javascript assigns undefined to variables or object's properties which declared but not assigned any value.

a.test1 = undefined;

console.log(test1); // undefined
console.log(a); // { test1: undefined }

In the above snippet, you are specifically assigning undefined to test1 property of a. If you don't assign any value then it will contain undefined by default JS behavior as shown below

a.test1; 
console.log(a); // { test1: undefined }

One more interesting feature of undefined is with JSON. JSON.stringify will ignore all properties of object which are undefined.

let obj = {a:1, b:undefined};
console.log(JSON.stringify(obj)); // {a:1} 

For more about primitive types undefined

Akshay Bande
  • 2,491
  • 2
  • 12
  • 29
  • 1
    first Thing `console.log(test1)` throw an error while you run this statement second `a.test1;` is in invalid statement and third thing is `undefined` and `null` treated different check this `let obj = {a:1, b:undefined, c: null}; console.log(JSON.stringify(obj));` – Manjeet Thakur Jan 29 '20 at 04:56
  • @ManjeetThakur Yes, you are right. But anyhow, you both solved my problem.. Thanks – tbhaxor Jan 29 '20 at 09:51
  • @GurkiratSingh my answer assumed that you have already declared `a` object. – Akshay Bande Jan 29 '20 at 10:55
0

undefined is a falsy value in Javascript. If a variable is not assigned with some value, then JS automatically assigne a value of undefined.

let a = {};

In this case, the object is blank. And accessing any value of a would give undefined. However, if some values are assigned to a, in your case

a.test1 = undefined;

In this, a has some values, so a.test1 will not give you undefined that was assigned by default to the variable as you have already assigned some values to it. And on accessing it, you will get the value assigned i.e undefined

Himanshu
  • 71
  • 3