Javascript Scope Exercises


1. Determine what this Javascript code will print out (without running it): x = 1; var a = 5; var b = 10; var c = function(a, b, c) { var x = 10; document.write(x); document.write(a); var f = function(a, b, c) { b = a; document.write(b); b = c; var x = 5; } f(a,b,c); document.write(b); } c(8,9,10); document.write(b); document.write(x); }

Solution:- 10, 8, 8, 9, 10, 1.


2. What is the difference between a method and function?

Solution:-A function is block of code that perform certain task. while, Method is a property of an object that contains function definition. Example:- function sum(a,b){ return a+b; } let console={ a:10, b:20, product:function(x,y){ return x*y; } } sum(10,20) //This is way to call function console.product(10,20) // This is way to call Method


3. What does 'this' refer to when used in a Java method?

Solution:-'this' keyword in Java refers to the current object of the function. The value of 'this' always similar. Doesn't matter which function is calling it.


4. What does 'this' refer to when used in a JavaScript method?

Solution:-In JavaScript, All code live inside the object and all code run inside 'global' object. 'this' keyword in javascript refers to the object who called the function. The value of 'this' ma be change as per respcet the caller function.


5. What does 'this' refer to when used in a JavaScript constructor function?

Solution:-In a constructor 'this' refers to a new object. when new ovject is created.


6. Assume object x is the prototype for object y in Javascript. Object x has a method f( ) containing keyword 'this'. When f is called by x.f( ), what does 'this' refer to?

Solution:-'this' ref to 'x'.


7. What is a free variable in JavaScript?

Solution:-A free variable is the variable that used inside a function but not declared in that function. Example:- let x = 10; function sum(a,b){ return a+b+x; } sum(10,20); 'x' is the free variable in above example.


8. Create an object that has properties with name = "fred" and major="music" and a property that is a function that takes 2 numbers and returns the smallest of the two, or the square of the two if they are equal.

Solution:- let a = { name : "fred", major : "music", smallestNum : function(a, b){ if (a==b) return Math.sqrt(a*b); else if (a>b) return b; else return a; } }


9. Write Javascript code for creating three Employee objects using the "new" keyword and a constructor function. Employee objects have the following fields: name, salary, position.

Solution:- function Employee(name, salary, position){ this.name = name; this.salary = salary; this.position = position; } Employee e1 = new Employee("Suresh", "200K", "CTO"); Employee e2 = new Employee("Ramesh", "150K", "Product Manager"); Employee e3 = new Employee("Kamlesh", "100K", "Senior Engineer");


10. Write a Javascript function that takes any number of input arguments and returns the product of the arguments.

Solution:- function product(){ let len = arguments.length; if(len<1)return 0; let output = 1; for(let i =0; i<len; i++){ output*=arguments[i]; } return output; }


11. Write an arrow function that returns the maximum of its three input arguments.

Solution:- let findMax = (a,b,c) => { if(a>b && a>c) return a; else if (b>a && b>c) return b; else return c; }