Monday, 13 August 2018

Is it true that Primitive Data Types in Javascript are Primitive?



Like other programming languages, Javascript also has a primitive data type. There are several primitive data types in Javascript
1. null
2. undefined
3. string
4. number
5. boolean
6. symbol (only in Ecmascript 2015)

Yes ... JavaScript has primitive data types ... But behind the scenes, the JavaScript engine prepares access to various methods / features in the built-in Object according to its type.

Example

let name = "Agus";
Above is a string primitive data type ... but behind the screen, Javascript
provides a special object / copy of the variable name that has access to all the built-in String () functionality because the type is string.

Therefore, the above name variable will have access to functions / methods such as:

name.toUpperCase (); // AGUS
name.toLowerCase (); // agus
Another example.

let age = 20;
age variable is an integer primitive, and integer is Number.
Javascript will create a special object / copy of the variable age
and to the special object earlier, then provides the functionality of the built-in Number (), so he will have access to functions like

age.toFixed (2); // 8:00 p.m.
to see what methods the access is opened, just check through the browser console


console.dir (name .__ proto__);
console.dir (age .__ proto__);
How come proto? yes the mechanism is as rich as prototypal inheritance, but specifically for primitive data types, the object / copy special is immediately destroyed from memory by the garbage collector

Special Apasih Object? , it is the invisible invisible mechanism that serves as a "temporary" object that can inherit methods / functions from its parent object. Another example ...

let name = "Budi";
When we make a variable name = "mind", javascript will create a temporary object with the value "mind". say for example: [temporary] = "mind", and it turns out that the type is a string, therefore [temprorary] will inherit all the functionality of String () or in other words the [temprorary] inherits the protototype property from String ().

Dizzy? what is the proof? check the following code

let name = "Budi";

let x = name.toUpperCase (); // will generate a new string
console.log (x); // BUDI
console.log (name); // Budi
Well it looks right, if we access console.log (name) again; then the name of the contents remains the same, namely "Budi"

Share this


0 Comments