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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read