In this article, we learn how JavaScript primitive/object types passed through functions. First, we will see, what are primitive and object data types in JavaScript:
1. Primitive data type: The predefined data types that are provided by JavaScript are called primitive data type. The primitive data types in JavaScript are:
- String: A String is a collection of characters that are written in double (“”) or single (”) quotes in JavaScript.
- Number: There are 2 types of numbers in JavaScript, with decimal or without decimal.
- Boolean: It only accepts true and false.
- Null: It represents a null or empty value.
- Undefined: When the value of a variable is not assigned, the value of that variable will be undefined.
2. Object data type: It falls under the non-primitive data type. Data types derived from primitive data types in JavaScript are called non-primitive data types. Both object and array fall under non-primitive data type but here we will only look at the object data type.
Example: The below example shows all the above data types in JavaScript.
Javascript
<script>
let s = "Hello"
let p = 'Hii' ;
console.log(s);
console.log(p);
let q = 10.65;
let r = 20;
console.log(q);
console.log(r);
console.log(10>9);
console.log(9>10);
let z = null ;
console.log(z);
let x;
console.log(x);
let car = {
name: 'TATA' ,
color : 'red' ,
}
console.log(car.name);
console.log(car.color);
</script>
|
Output:
Hello
Hii
10.65
20
true
false
null
undefined
TATA
red
Now let us see how primitive/object data types are passed through functions in JavaScript. First, we will see how to pass primitive types in function then we will look at the object.
Passing primitive types in function: Primitive types are always passed by value. These are immutable. This means that even if we change the value in the function, the original value will not change. There are a total of five primitive data types in JavaScript. The below examples will demonstrate this approach.
Example 1 (String): We are passing the two string values or parameters through the function print.
Javascript
<script>
function print(y, z) {
return y + " " + z;
}
let x = print( "Hello" , "everyone" );
console.log(x);
</script>
|
Output:
Hello everyone
Example 2 (Number): We are passing the number (decimal and without decimal) through the function print.
Javascript
<script>
function print(y, z) {
console.log(y);
console.log(z);
console.log(y + z);
}
print(10, 20.5);
</script>
|
Output:
10
20.5
30.5
Example 3(Boolean): We are passing the number through the function print and it returns “true” if the condition is satisfied otherwise it returns “false”.
Javascript
<script>
function print(y, z) {
return y > z;
}
console.log(print(50, 40));
</script>
|
Output:
true
Example 4(Null): We are passing the null variable through the function print and null itself is printed.
Javascript
<script>
function print(y) {
console.log(y);
}
let x = null ;
print(x);
</script>
|
Output:
null
Example 5 (Undefined): Here we are passing the undefined variable through the function print.
Javascript
<script>
function print(y) {
console.log(y);
}
let x;
print(x);
</script>
|
Output:
undefined
Passing object type in function: Objects are passed by reference. If the property of the object in the function is changed, then the original value also changes.
Example: In the below example, we create a laptop object, which has been passed to the function print.
Javascript
<script>
let laptop = {
name: "Dell" ,
color: "black" ,
quantity: 1
};
print(laptop);
function print(obj) {
console.log(obj.name);
console.log(obj.color);
console.log(obj.quantity);
}
</script>
|
Output:
Dell
black
1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Mar, 2022
Like Article
Save Article