Javascript Core Concepts
1. Value Type and Reference Type
In JavaScript, a variable may store two types of values: primitive and reference. JavaScript provides six primitive types as undefined, null, boolean, number, string, and symbol , and a reference type object.
The size of a primitive value is fixed, therefore, JavaScript stores the primitive value on the stack.On the other hand, the size of a reference value is dynamic so JavaScript stores the reference value on the heap.
When you assign a value to a variable, the JavaScript engine will determine whether the value is a primitive or reference value. If the value is a primitive value, when you access the variable, you manipulate the actual value stored in that variable. In other words, the variable that stores a primitive value is accessed by value. Unlike a primitive value, when you manipulate an object, you work on the reference of that object, rather than the actual object. It means a variable that stores an object is accessed by reference.
To determine the type of a primitive value you use the typeof operator. For example:
let x = 10;
console.log(typeof(x)); // number
and
let str = ‘JS’;
console.log(typeof(str)); // string
To find the type of a reference value, you use the instanceof operator:
let refType = variable instanceof constructor;
For example, the following rgb variable is an instance of the Array object:
let rgb = [‘red’,’green’,’blue’];
console.log(rgb instanceof Array); // true
Copying primitive values
When you assign a variable that stores a primitive value to another, the value stored in the variable is created and copied into the new variable.
Let’s take a look at the following example.
First, declare a variable a and initialize its value to 10.
var a = 10;
Second, declare another variable b and assign it a value of the variable a. Internally, JavaScript engine copies the value stored in a into the location of b
var b = a;
Third, assign the variable b new value 20.
Since a and b have no relationship, when you change the value stored in the b variable, the value of the a variable doesn’t change.
console.log(a); // 10;
console.log(b); // 20;
Copying reference values
When you assign a reference value from one variable to another, the value stored in the variable is also copied into the location of the new variable.
The difference is that the values stored in both variables now are the address of the actual object stored on the heap. As a result, both variables are referencing the same object.
Consider the following example.
First, declare a variable x that holds an object whose name property is 'John'.
var x = {name: ‘John’};
Second, declare another variable y and assign the x variable to y. Both x and y are now referencing the same object on the heap.
var y = x;
Third, modify the value stored in the name property of the object using the y variable.
y.name = ‘David’;
2. JavaScript Primitive Wrapper Types
JavaScript provides three primitive wrapper types: Boolean, Number, and String types.
The primitive wrapper types make it easier to use primitive values including booleans, numbers, and strings.
See the following example:
let language = ‘JavaScript’;
let s = language.substring(4);
console.log(s); // Script
In this example, The variable language holds a primitive string value. It doesn’t have any method like substring(). However, the above code works perfectly.
When you call a method on a variable that holds a number, a string, or a boolean, JavaScript performs the following steps behind the scenes:
- Create an object of a corresponding type.
- Call a specific method on the instance.
- Delete the instance immediately.
So the following code
let language = 'JavaScript';
let str = language.substring(4);is technically equivalent to the following code:
let language = 'JavaScript';
// behind the scenes of the language.substring(4);
let tmp = new String(language);
str = temp.substring(4);
temp = null;Primitive wrapper types vs. reference types
When you create an object of a reference type using the new operator, the object will stay in the memory until it goes out of scope.
Get Rahul Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
The following variable s will stay on the heap until it goes out of the scope:
let s = new String('JavaScript');
console.log(s);However, an automatically created primitive wrapper object exists for one line of code only. See the following example:
let s = 'JavaScript';
s.language = 'ECMAScript';
console.log(s.language); // undefinedIn this example, we attempted to access the language property of the s variable and received a value of undefined instead of 'ECMAScript':
console.log(s.language); // undefinedThe reason is that the following code creates a String object and assigns a value to the language property.
s.language = 'ECMAScript';However, the String object with the language property only exists during the execution of this line of code.
It’s not recommended to explicitly create primitive wrapper objects like the following:
let n = new Number(10);
let s = new String('JS');
let b = new Boolean(false);However, you should know which methods are available for a primitive value in order to manipulate it more effectively.
3. JavaScript pass by value or pass by reference
In JavaScript, all function arguments are always passed by value. It means that JavaScript copies the values of the passing variables into arguments inside the function.
Any changes that you make to the arguments inside the function does not affect the passing variables outside of the function. In other words, the changes made to the arguments are not reflected outside of the function.
If function arguments are passed by reference, the changes of variables that you pass into the function will be reflected outside the function. This is not possible in JavaScript.
Passing by value of primitives values
Let’s take a look at the following example.
function square(x) {
x = x * x;
return x;
}
var y = 10;
var result = square(y);
console.log(y); // 10 -- no change
console.log(result); // 100How the script works.
First, define a square() function that accepts an argument x . The function changes the value of the x argument.
Next, declare the variable y and initialize its value to 10:
Then, pass the y variable into the square() function. When passing the variable y to the square() function, JavaScript copies the value of y to the x variable.
After that, the square() function changes the x variable. However, it does not impact the value of the y variable. This is because x and y are totally different variables. They have no link.
Finally, the value of the y variable does not change after the square() function completes.
If JavaScript uses passing by reference, the value of the variable y would change to 100.
Passing by value of object
It’s obvious to see that primitive variables are passed by values. However, it is not the case for objects. Take this for example:
function turnOn(machine) {
machine.isOn = true;
}var computer = {
isOn: false
};turnOn(computer);
console.log(computer.isOn); // true;
How the script works:
First, define the turnOn() function that accepts an object machine. The function sets the isOn property of the object to true.
Next, declare a variable computer and assign it an object whose isOn property is set to false.
Internally, the computer is a variable that references the actual object:
Then, pass the computer variable into the turnOn() function.
JavaScript copies the value of the computer variable to machine variable. As a result, both computer and machine variables are referencing the same object in the memory:
After that, inside the turnOn() function, the isOn property of the object is set to true via the machine variable.
Finally, accessing the isOn property of the computer variable returns true.
It seems that JavaScript passes an object by reference because the changes to the object are reflected outside of the function. However, this is not the case.
In fact, when you pass an object to a function, you are passing the reference of that object, not the actual object (computer). Therefore, the function can modify the properties of the object via its reference.
In addition, when you pass an object into a function, the function cannot change the reference variable to reference another object.
Let’s prove it through the following example:
function turnOn(machine) {
machine = {
isOn: true
};
}var computer = {
isOn: false
};turnOn(computer);console.log(computer.isOn); // false;
This time, the turnOn() function changes the machine argument so that it references another object.
Before passing the computer object to the turnOn() function, the isOn property of the computer object is false.
If the computer variable was passed by reference, the computer variable would be changed and referenced the new object whose isOn property is true.
However, when we access the isOn property outside of the function, the values is false. It indicates that the original reference did not change even though the argument was changed inside the function.





