How to pass primitive/object types through functions in JavaScript ?
Last Updated :
09 Mar, 2022
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>
// Primitive Datatype
// String
let s = "Hello"
let p = 'Hii';
console.log(s);
console.log(p);
// Number
let q = 10.65;
let r = 20;
console.log(q);
console.log(r);
// Boolean
console.log(10>9);
console.log(9>10);
// Null
let z = null;
console.log(z);
// Undefined
let x;
console.log(x);
// Object Datatype
// Car is a object
let car = {
name: 'TATA',
color : 'red',
}
// Calling the object data and print them
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;
}
// Here the function print is called and
// the argument is passed and store the
// value in x
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);
}
// Here the function print is called
// and the argument is passed
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;
}
// Call the function print and passing
// arguments and the function return
// true or false
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);
}
// Here value is not assigned
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>
// Create a laptop object
let laptop = {
name: "Dell",
color: "black",
quantity: 1
};
// Call the function print where
// we pass the laptop object
print(laptop);
// Print the object
function print(obj) {
console.log(obj.name);
console.log(obj.color);
console.log(obj.quantity);
}
</script>
Output:
Dell
black
1
Similar Reads
How a Function Returns an Object in JavaScript ?
JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in Jav
3 min read
How to Store an Object sent by a Function in JavaScript ?
When a function in JavaScript returns an object, there are several ways to store this returned object for later use. These objects can be stored in variables, array elements, or properties of other objects. Essentially, any data structure capable of holding values can store the returned object. Tabl
2 min read
How to Pass Object as Parameter in JavaScript ?
We'll see how to Pass an object as a Parameter in JavaScript. We can pass objects as parameters to functions just like we do with any other data type. passing objects as parameters to functions allows us to manipulate their properties within the function. These are the following approaches: Table of
3 min read
How to check if the value is primitive or not in JavaScript ?
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results. Primitive data types are basic building blocks like numbers and characters, while non-primitive data
3 min read
How to read properties of an Object in JavaScript ?
Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to convert an object to string using JavaScript ?
To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp
4 min read
How to invoke a function without parenthesis using JavaScript ?
Method 1: Using the new operator. The new operator is used to create an instance of an object which has a constructor function. This constructor function can be used to write our own function and then be invoked by the new operator. The parenthesis is optional when using this operator, hence the fun
2 min read
How to create an object with prototype in JavaScript ?
In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
4 min read
How to Store a JavaScript Fnction in JSON?
In JavaScript, we can store JavaScript functions in JSON objects, allowing us to serialize and store executable code alongside data. we will explore three approaches to store a JavaScript function in JSON. These are the following approaches: Table of Content Using JSON.stringify()Using a Custom toJS
2 min read
How to check the type of a variable or object in JavaScript ?
In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read