JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – null and undefined act as value in function parameter

JavaScript Quiz – null and undefined act as value in function parameter

JavaScript Function With null and Undefined

In javascript, a function is a highly used keyword due to its reusability and functionality of maintaining code. The function can have a default value in the parameter but if null and undefined pass as value. As there are multiple ways to declare function in javascript.

In which one of them are functions with a default parameter. That means if no value pass to function as parameter then default is used.

A JavaScript function is a block of code designe to perform a particular task. A JavaScript function is execute when “something” invokes it (calls it).

Basic Syntax of Function With Parameter

function fnName(variable = "value"){
...
}

For more detail about the javascript function click here.

JavaScript Question

In this javascript quiz, let’s find does null and undefined act as value in function parameter brcause function have default parameter value.

First, we declare a javascript function with name logName which is a parameterised function with default value.

JavaScript Function

function logName(name = "Ajay"){
console.log(name);
}

As you can see, in the function body we are console log the name value. Which can be “Ajay” is no parameter is pass or the value which pass to function.

Now, we are passing null value and then undefined.

logName(null);
logName(undefined);

Any idea what should be the value of above code statement.

JavaScript Quiz Test

Here is the full code snipppet which you need to answer.

function logName(name = "Ajay"){
console.log(name);
}

logName(null);             // output => ?
logName(undefined); // output => ?

Answer – null, “Ajay”

Click To Run Code

Explanation

Now, you know the answer is null, “Ajay”. But why doesn’t it should give “Ajay“, “Ajay“. Let me explain.

As, when no value pass to function with default parameter value then it  set explicitly to undefined. Because of it, the default value is used.

But for other falsy value like null, ‘ ‘ is not explicitly converted to undefined. Due to which it does not trigger default value to set to variable when null is passed.

Here you check an example of a default function parameter with null or undefined. Click here.

At last, That’s why we get an answer null “Ajay.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – array map converts an element value with parseInt
  2. JS Quiz – does the splice method replace array element
  3. JS Quiz – is the console log function really override
  4. JavaScript Quiz – delete work on object create method object

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link