4

Trying to understand the difference between null and undefined in JavaScript I came across this answer where it mentions

null is an assignment value. It can be assigned to a variable as a representation of no value

The question is, once you define a variable like:

var someVariable = null;

Isn't someVariable defined some place in the memory? then shouldn't it have random 0s and 1s already in it unless assigned specific value otherwise? On a bit level what does null represent? Does it mean bits empty of 0 or 1?

ehsan0x
  • 660
  • 5
  • 10
  • 24
  • 2
    Exactly what is put in memory for a particular value will vary depending on which JavaScript engine is being used (e.g. V8 in Chrome, SpiderMonkey in Firefox, etc.). – Harry Cutts Sep 18 '18 at 21:10
  • JS is a high-level language and is not concerned with what bits are located at which place in the memory. `null` is just another primitive value, like `NaN` or `0` or `27`. – Bergi Sep 18 '18 at 21:32
  • "*shouldn't it have random 0s and 1s already in it*" - no, it should not. It should have a definite value (which is either `undefined` or "throw a reference error"). We don't want it to have an arbitrary (more or less random) bit pattern that would represent an actual value in our program. JS does have more types than just bytes, and an object reference pointing to an arbitrary place in memory could wreak quite some havoc. – Bergi Sep 18 '18 at 21:35

2 Answers2

3

You seem to be confused by that answer stating that "null is not a value", or that it means "a variable having no value". That's wrong, and that's not what it says.

The null value is very much a value in the domain of the JS language. It's a primitive value just like undefined, true, NaN, 0 or 42.

The answer you quoted says that this value can represent a "no value" in your application domain. Putting the value null there means that it has no other value, e.g. being "neither true nor false".

(And whichever JS value we are looking at, we are not concerned with its bit-level representation in memory, like which bit exactly would distinguish a string from an integer. They're just values that we can work with in our program, and there are no others.
In fact a let variable in JS can indeed have a state of being uninitialised, but that doesn't mean there is a random bit pattern we can read back, but rather it's an explicit state that causes the program to throw an exception when the variable is evaluated. And var variables are always initialised, with the undefined value when a scope is created.)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Firstly, when programming in JS, you should never worry about what exactly is held in memory when you declare a variable. It is a high-level language with automatic memory management (including garbage collection) - not like C where I believe you can deal with the actual memory if you want to. (NB I've never programmed in C or a low-level language like it, so anything I say about that language should be taken with more than a grain of salt.) JS variables are just an abstraction where you use a name as a "container" for a particular value (which can change over time - hence the name "variable").

As for why JS has two "nothing" values in null and undefined - this is a very good question, and is likely just a quirk of the language. undefined is the best representation of "no value", since it's the value that declared variables will hold before they've been given another value, the default return value from a function where you just put return; or leave off the return statement, and the value automatically assigned to any function parameters which are in the function's signature but not passed in when called.

As for null, I'm not sure why it needs to exist - but it has since the beginning. Largely by convention, it tends to be used by developers of certain libraries, in places where an object is expected but none actually exists. (Despite the fact that, as I commented on another answer, null is not itself an object vale.) MDN, for example, highlights this usage: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null#Description

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34