Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JavaScript store null values internally?

I always wondered how JavaScript stores null values internally. null is different than any value. I imagine that the scope's variables are stored in some kind of array of structures, each structure corresponding to a variable. Does this structure has some kind of boolean property called "null"?

I would look for it myself in the V8 source code, but am am pretty lost in the C++ code :(

I couldn't find anything on Google. Most results were related to questions like "How to determine if variable is undefined or null?" or similar.

like image 436
Aalex Gabi Avatar asked Dec 27 '12 00:12

Aalex Gabi


People also ask

How does null work in JavaScript?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

How do you handle a null value in JavaScript?

JavaScript uses the null value to represent a missing object. Use the strict equality operator ( === ) to check if a value is null . The typeof null returns 'object' , which is historical bug in JavaScript that may never be fixed.

Does null exist in JavaScript?

'Null' in JavaScript'Null' is a keyword in JavaScript that signifies 'no value' or nonexistence of any value. If you wish to shred a variable off its assigned value, you can simply assign 'null' to it. Besides this, like any other object, it is never implicitly assigned to a variable by JavaScript.

Does null return TRUE JavaScript?

Another curiosity is that when you loosely check for equality using double equals == , null and undefined will return true . But when you strictly check for equality using triple equals === , null and undefined will return false . This is because null and undefined are both falsy in JavaScript.


1 Answers

JavaScript, being a weakly-typed scripting language, probably has a variant type that supports values of all kinds of types. I believe PHP uses the same type of variables, although they are known in other languages too and play an important role in COM automation.

They are basically structs that contain a type and a value. The value can be a simple value, or a pointer to a larger or more complex value. null is just one of the supported types, in which case the value itself plays no part.

But since JavaScript knows many interpreters, there is no rule about how it is implemented, and it is something you should not worry about. Normally you would only need to know about underlying techniques like this for time-critical applications, which should not by written in JavaScript anyway.

like image 52
GolezTrol Avatar answered Sep 18 '22 02:09

GolezTrol