JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – function add without return keyword can sum

JavaScript Quiz – function add without return keyword can sum

Function Without Return Keyword

The javaScript function is a block of code that is known for its reusability. To declare a javascript function a function keyword with its name is needed. and return keyword play main role as it return the output.

Function also have () invoke method which trigger a function.

Basic Syntax to javascript function

function validFunctionName(parameter){
    return statement;
}

For more detail to declare javascript function

JavaScript Question

In this javascript quiz, find out does function add without return keyword can sum because function can add two values but return needed.

First, we declare a function with name add and pass two parameter in it. Inside the function code, we are adding both of the parameter.

function add(num1, num2){
    num1 + num2;
}

Now, we call add function with passing two numeric value to it which are 2 and 2.

And also printing the result return from function add using console log.

console.log(add(1,2));

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

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

function add(num1, num2){
    num1 + num2;
}

console.log(add(1,2));
// output => 🧐 ?

// Made with ❤️, by @jsstartup

Answer – undefined

Click To Run Code

Explanation

Now, you know the answer is undefined. But why doesn’t it add those value or either concatenate them. Let me explain

InShort, javascript function is without a return keyword due to which nothing is return from the function.

And when you got nothing from function then you have undefined as a return value.

At last, That’s why we get an answer “undefined”

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. JS Quiz – array element duplicate count with for loop
  2. JavaScript Quiz – isArray method and typeof intend is different
  3. JavaScript Quiz – does replace really work on replaced string
  4. JS Quiz – string length and number length what they return?

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