Based on the following code var a 5 function someFuncxy va

Based on the following code:

   var a = 5;

   function someFunc(x,y) {
var a = 2 + y;
return a;
}
  
a = someFunc(a,3);

What is the final value of the first variable a. Including initialization, how many times was its value changed. Why?

A.
Final value: 5

Changes: 2

Why: The variable inside the function someFunc, which also happens to be called a, is not the same a as the one declared outside someFunc.

B. Final value: 10

Changes: 4

Why: The variable a inside someFunc is the exact same variable from outside someFunc

C. Final value: 3

Changes: 2

Why: Declaring a variable with the same name, overrides the original global variable.

D.
Final value: 5

Changes: 1

Why:  When the returned value of someFunc is assigned into a at the last line.

Solution

Option is A.

The two variables outside the someFunc and in the someFunc are different. So, the value has been changed twice and the function just adds y to 2 and we are assigning y as 3 when the function is called therefore at this time value of \'a\'= 5 and it is returned.

Based on the following code: var a = 5; function someFunc(x,y) { var a = 2 + y; return a; } a = someFunc(a,3); What is the final value of the first variable a.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site