DEV Community

Cover image for Technical Issues In Javascript.2
Emmanuel Onah
Emmanuel Onah

Posted on • Updated on

Technical Issues In Javascript.2

Technical Issues Figured Out In Javascript | Behind The Hood Issues

TABLE OF CONTENT

  1. Primitive vs Reference types

  2. typeof in javascript

  3. Expression and Statement

1. Primitive vs Reference types

Before I proceed with the discussion, I will love to brief you on the history of how the English/German word primitive was use to classify some set of data types. Back in the early days of programming, programmers/coders/developers/engineers used to store values to a variable be it 1000 values, they will create variables for each of those values. But as development went on, some smart persons thought that why don't we store some values as a group because it doesn't make sense storing values of the same data-type in different variables. So they came about the array and as time goes on, they thought about storing values that belong to one single program or variable as objects or property of that variable or program and that was how OOP got their shot from.

I guess now you have remembered how the word primitive came about (Something related to early days). Now let's proceed!

i. Primitive data-types:

From the material you have re(a)d or from the google explanation, you will see something like this:

Alt Text

Wow its quite interesting that bigInt has now been added as a primitive data-type in js but mind you, it's still classified as a number!

From my own definition, a primitive data-types are those data that get allocated into the RAM space(an area in the memory called the stack(an area that is small compared to the heap where reference types are stored)) with respect to its variable name. The example of the primitive types is a string, number, boolean, null, undefined, symbol, and object.

So what's my point here?

My point here is that any data that gets into a stack-space and a name is been allocated to it inside the stack for easy access, that data is a primitive data type. So the argument here is that; are reference types an implicit-primitive type? The answer is YES!!! But you will never see a material telling you that objects and arrays are primitive types(a lot of historical bug in programming and in this article we will find out some of them just be calm and follow).

ii. Reference types

So what are reference types?

Alt Text

The above picture is another one from google, which i guess is a bit unclear but never mind we will figure it out right away.

My short description of reference types is that: a reference type is a type of data in which its process of storage is divided into two phases: the primitive phase(the super phase) and the reference phase(supperless phase). The primitive phase holds the main data of the external variable and The reference phase holds the external variable that holds the properties inside the Primitive area(The stack). Note, all that the reference phase knows is just the location the values are stored inside the Primitive phase.

It's okay to be confused with all those foo-bar-baz but let's look at the code snippet with explanations below:

Alt Text

Alt Text

Alt Text

Alt Text

So, I hope you have a bit clue of how data management works between the primitive and reference type? But always remember that the heap holds the pointers whos main value resides inside the stack memory.

2. typeof in javascript

typeof is just another implicit operator that returns the data-type that a variable holds! mind you, in javascript we don't have a variable-type but rather an accessor type(const, let or var) and we only have data types for the values of a variable. It's only in languages like java that you have a variable type like int, long, string, and so on and so fort(the variable int must hold a data-type int in java).

So what are we going to discuss here? well, i decided to add this topic to this article because many people coming into javascript will always ask themselves and get confused on why some certain variables return a different data-type when you type typeof e.g: null as a value of a variable returning an object as a data-type whereas, null is clearly mentioned as a primitive data-type. You see why i told you that there are some unquestionable bugs in javascript:)

Let's take a look at the snippet below
Alt Text
Alt Text

i. The null data-type:

For beginners, don't mind javascript :).You will be told that null is a primitive data type on its own but when you use the typeof operator to get its data-type, javascript will contradict itself and tell you that null is an object and not a null????? Wait wait wait; null data-type is an object but i thought null is a primitive type and object is a reference type????? Now you see why i told you to pack the reference keyword aside and let's face the almighty primitive data-type hahahaha. We don't know why javascript decided to tips us up like this but we hope to find out as the journey continues hahaha

ii.The array-data-type:

For beginners in javascript, they will say i thought array is a type of reference type and object is another type of reference type but why is the same javascript confusing us???? hahaha I will tell you not to mind javascript because javascript is full of fun just love it the way you see it but if you have the strength to change it, then go meet Mozilla organization and propose to be a contributor of js, probably you will be opportune to change it(:))

iii.The undefined data-type:

I could remember when i started writing java, all the documentation and lectures I had, I ended up coming up with the below analogy which is true!:

Alt Text

But why will javascript tell us that when a variable doesn't exist its data-type is equal to undefined and when a variable exists but doesn't have value its data-type is still equal to undefined ???:). I tell you again not to mind javascript hahaha its fun just write it and know your way around!

3.Expression and Statement:

i. Expression:

An expression in javascript is derived from the English word phrase which means a piece of a sentence that makes part of a sentence(most times they don't make sense standing by themselves and doing nothing).

ii. Statement:

Statement in javascript is derived from the English word sentence which means; a group of words that are composed(similar to compose in redux-thunk) together to make a complete sense.

So you bring an expression together to perform an action in your program. So note; that when you just declare and define a variable, it's just an expression and ones you use it in another section with another variable but it mustn't be another variable (maybe just a simple concatenation), it becomes a statement

Alt Text

Previous Content

Next Content

Top comments (3)

Collapse
 
kimsean profile image
thedevkim

I really like these kind of post! Thank you for sharing!

Collapse
 
emmanuelonah profile image
Emmanuel Onah

Thanks thedevKim for reading this article,

let's keep making programming easy and fun for everyone who wants to join the hood.

Collapse
 
kimsean profile image
thedevkim

Yes, everything's become easier if a person's having fun!