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

javascript21.Which type of variable among global and local, takes precedence over other if names are same?
A local variable takes precedence over a global variable with the same name.

22.What is callback?
A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

23.What is closure?
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.

24.Give an example of closure?
Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −

function create() {
var counter = 0;
return {
increment: function() {
counter++;
},

print: function() {
console.log(counter);
}
}
}
var c = create();
c.increment();
c.print(); // ==> 1

25.Which built-in method returns the character at the specified index?
charAt() method returns the character at the specified index.

26.Which built-in method combines the text of two strings and returns a new string?
concat() method returns the character at the specified index.

27.Which built-in method calls a function for each element in the array?
forEach() method calls a function for each element in the array.

28.Which built-in method returns the index within the calling String object of the first occurrence of the specified value?
indexOf() method returns the index within the calling String object of the first occurrence of the specified value, or −1 if not found.

29.Which built-in method returns the length of the string?
length() method returns the length of the string.

30.Which built-in method removes the last element from an array and returns that element?
pop() method removes the last element from an array and returns that element.

You may also like

Leave a Comment