Home Interview Questions and Answers JavaScript Interview Questions and Answers Part-2

javascript11.How many types of functions JavaScript supports?
A function in JavaScript can be either named or anonymous.

12.How to define a anonymous function?
An anonymous function can be defined in similar way as a normal function but it would not have any name.

13.Can you assign a anonymous function to a variable?
Yes! An anonymous function can be assigned to a variable.

14.Can you pass a anonymous function as an argument to another function?
Yes! An anonymous function can be passed as an argument to another function.

15.What is arguments object in JavaScript?
JavaScript variable arguments represents the arguments passed to a function.

16.How can you get the type of arguments passed to a function?
Using typeof operator, we can get the type of arguments passed to a function. For example −

function func(x){
console.log(typeof x, arguments.length);
}
func(); //==> “undefined”, 0
func(1); //==> “number”, 1
func(“1”, “2”, “3”); //==> “string”, 3

17.How can you get the total number of arguments passed to a function?
Using arguments.length property, we can get the total number of arguments passed to a function. For example −

function func(x){
console.log(typeof x, arguments.length);
}
func(); //==> “undefined”, 0
func(1); //==> “number”, 1
func(“1”, “2”, “3”); //==> “string”, 3

18.How can you get the reference of a caller function inside a function?
The arguments object has a callee property, which refers to the function you’re inside of. For example −

function func() {
return arguments.callee;
}
func(); // ==> func

19.What is the purpose of ‘this’ operator in JavaScript?
JavaScript famous keyword this always refers to the current context.

20.What are the valid scopes of a variable in JavaScript?
The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.

Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

You may also like

Leave a Comment