Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to pass primitive/object types through functions in JavaScript ?

How to pass primitive/object types through functions in JavaScript ?

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

RELATED ARTICLES

Most Popular

Recent Comments