MCQ Practice Single Best Answer Topic: Javascript Graphics and Rendering MCQ

Q What is the result of the following JavaScript code?

<p id="demo"></p>
<script>
function myFunction() 
{
   var num = 3+2;
   var n = num.valueOf()
   document.getElementById("demo").innerHTML = n;
}
</script>

Question ID
#5249
Subchapter
Javascript Graphics and Rendering MCQ
Action
Choose one option below

Choose Your Answer

Click an option to check whether your answer is correct.

  • A 5
  • B 2+3
  • C Error
  • D Undefined
Correct Answer: A

Explanation

The code snippet you provided is a simple JavaScript function called "myFunction". This function performs the following actions:

  1. The first line inside the function declares a variable called "num" and assigns it the value of the result of the mathematical expression 3 + 2, which is 5.

  2. The next line declares a variable called "n" and assigns it the value of the primitive value of the "num" variable by calling the valueOf() method.

  3. The final line uses the JavaScript DOM method getElementById() to select the HTML element with the id "demo" and then sets the innerHTML property of that element to the value of the "n" variable.

  4. The function does not call itself and it need to call it to execute the code, for example myFunction()

In this case, when the function is called it will select the HTML element with the id "demo" and set its innerHTML to the value of the variable "n", which is 5. So, the output will be 5 in the element with id "demo" on the web page.

Share This Question

Share this MCQ with your friends or study group.