

Is NULL Nothing? 🤔
while talking about NULL there is nothing better than C to understand it
in C null is just a macro nothing else
what is macro?
macro is a preprocessor text substitution
and preprocessor is a program that runs before our program passed to the compiler
example macro in C:
in the statement PI is called macro template and 3.1428 is the macro expansion
in C null is typically defined as
in some cases
NULL expands to 0 (or equivalent),
which the compiler converts into a null pointer value.
when we assign NULL to something C's preprocessor converts that NULL to 0 before compilation
for compiler null is just 0
normally if we do
this will convert 4.5 to 4 as int value
but when we try to convert to a pointer value
(void*) tells the compiler convert/interpret 0 as a pointer type
converting 0 as pointer type produces a special pointer value
if you do:
in most OS, you will get an error:
A segmentation fault occurs at runtime when a program tries to access or modify memory that it is not allowed to use.
NIL in Golang
in Golang we get nil
nil isn't literally 0 in golang
nil is predeclared identifier in go
that means we can use it anywhere in our code without declaring it
nil represent zero values of many type
if we look at the source code we will see
Type must be one of pointer, channel, func, interface, map, or slice type
you can think of it conceptually:
- no pointer
- no channel
- no function
- etc...
if you do
and try to access it
you will get an err
you can see the address is 0x0 similar to C
but as C you can't access it from your program
but if you do
it will print an empty slice.. here is nothing related 0 address
also you can append, remove value from the slice
so nil in Go behaves differently depending on the type using it
Let's see how javascript sees NULL 👀
Let's start with a javascript bug 😂
if you execute
you will get "object" as result
early javascript engines stored values in memory using small type tag system
The type tag was 0 of object and null was represented as NULL pointer (0x00 in most cases) so null got 0 type tag, hence the typeof null returns "object" as result
unlike Go's nil null isn't an identifier, null in javascript a syntax keyword and primitive value
if we do
that means the user exists, but currently no object value is assigned to it.
look at here for more details about null and undefined ->
So, null isn't an universal thing.. It depends on the language, runtime, OS, CPU architecture, and memory model.
Thanks for reading.. ❤️