10

why does javascript have null and undefined? they both seem to mean the same thing, there's nothing here and they are both falsey, as they should be. But it means, for example if I want to check if something exists but it could be {}, [], 0 or some other falsey thing i have to check

if(thing !== undefined && thing !== null)

also, I know that typeof null is Object but typeof {} is also Object while typeof undefined is "undefined" but

const nullVariable = null;
console.log(nullVariable.x) // => error
const emptyVariable = {};
console.log(emptyVariable.x) // => undefined
const undefinedVariable;
console.log(undefinedVariable.x) // => error

also, undefined is supposed to mean that the variable hasn't been declared yet, but you can declare a variable like

const undefinedVariable = undefined;

so it has been defined but it has not yet been defined?

so what I am saying is while they have different semantic meanings, one means a variable that hasn't been declared and on means a variable with no value, they seem to have the functionality and they are both false, trying to get a property form them will return an error.

basically what I am asking is why do we need both, why not just use one like python with None or lower level languages like java and c++ with Null?

david snyder
  • 337
  • 3
  • 16
  • Not Really, they are two completely different things. – Mosia Thabo Apr 05 '20 at 01:00
  • 3
    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) – Mosia Thabo Apr 05 '20 at 01:01
  • @MosiaThabo, no I read that post before I made this one, and that states the difference between but not the point of having both of them – david snyder Apr 05 '20 at 01:02
  • 2
    @ggorlen that was meant to display how it is unintuitive that `typeof null` is `Object` but you can't access properties from it, just like you can't access properties from undefined, I can see how you are confused so I'll edit the question. – david snyder Apr 05 '20 at 01:06
  • 1
    Ah, I see what you mean. Apparently the explanation for that is [here](https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript#comment9782995_5076962). – ggorlen Apr 05 '20 at 01:07
  • 1
    That point is also addressed in the duplicate ... – Don't Panic Apr 05 '20 at 01:26
  • Related: https://2ality.com/2013/05/history-undefined.html – jarmod Apr 05 '20 at 04:17

2 Answers2

5

I suggest you think of their pusposes to better understand the difference.

The value null represents the intentional absence of any object value. It's never assigned by the runtime.

Meanwhile any variable that has not been assigned a value is of type undefined. Methods, statements and functions can also return undefined. You also get undefined when you call a non-existent property or method of an object.

undefined has nothing to do with an empty value. For example:

console.log(5 + undefined);
// expected output: NaN
console.log(5 + null);
// expected output: 5

The distinction between both is useful given that JavaScript is dynamically typed and objects are dynamic "bags" of properties that can be changed at runtime.

let car = {type: "Fiat", model:"500", color:"white"};
console.log(car.type);
// expected output: "Fiat"
console.log(car.price);
// expected output: undefined
car.price = null;
console.log(car.price);
// expected output: null
car.price = 2000;
console.log(car.price);
// expected output:2000
dbaltor
  • 2,737
  • 3
  • 24
  • 36
2

imagine a glass of water;

if it exists, but doesn't have any water inside; it is null.

if there is no glass at all; it is undefined.

Mechanic
  • 5,015
  • 4
  • 15
  • 38
  • 3
    yes but in python, if you have a glass of water, it exists and there is no water in it then it is `None` and if there is no glass it is also `None` and there are no errors that come up due to that fact, also you would think, if the glass exists but has no water then `glass.water` would return undefined but it returns an error, `can not get property "water" of null` – david snyder Apr 05 '20 at 01:35
  • 2
    @davidsnyder two different errors, thrown when you call `glass.water` on `null` glass or `undefined` glass. one is `typeError`, the other is `referenceError`. – Mechanic Apr 05 '20 at 01:40
  • 2
    that's not the point, the point is they both throw errors. – david snyder Apr 05 '20 at 01:43
  • @davidsnyder dark-green and light-green both are `green`; but they are different. thats an old argument bro.. I suggest you read [YDKJS](https://github.com/getify/You-Dont-Know-JS) book by getify – Mechanic Apr 05 '20 at 01:45
  • you have a point there, but I just tested it again and they both return `TypeError` as seen [Here](https://i.imgur.com/f5rRG33.png) but even if they didn't thrown the same type of error that still doesn't explain the **need** for both of them – david snyder Apr 05 '20 at 01:53
  • 1
    @davidsnyder `let dd = null; dd.j` run this; and this `jj.d` nothing else; btw javascript is written in 10 days in the early days not event meant to go this far; but its architecture was brilliant.. reading a little about it's history may resolve some of this concerns :) – Mechanic Apr 05 '20 at 01:56