JavaScript Interview Questions
1 .
Explain the unshift() method ?
This method is functional at the starting of the array, unlike the push(). It adds the desired number of elements to the top of an array. For example :
 
var name = [ "free" ];
name.unshift( "time" );
name.unshift( "learning", "FTL" );
console.log(name);
 
The output is shown below:
 
[" free "," time ", " learning ", " FTL "]
2 .
What are the decodeURI() and encodeURI()?
EncodeURI() is used to convert URL into their hex coding. And DecodeURI() is used to convert the encoded URL back to normal.
 
<script>
	var url="my test.php?name=volkswagen&car=polo";

	document.write(encodeURI(url)+ "<br>");

	document.write(decodeURI(url));
</script>
 
Output:
 
my%20test.php?name=volkswagen&car=polo
my test.php?name=volkswagen&car=polo
3 .
What's the difference between event.preventDefault() and event.stopPropagation() methods?
The event.preventDefault() method prevents the default behavior of an element. If used in a form element it prevents it from submitting. If used in an anchor element it prevents it from navigating. If used in a contextmenu it prevents it from showing or displaying. While the event.stopPropagation() method stops the propogation of an event or it stops the event from occurring in the bubbling or capturing phase.
4 .
How to know if the event.preventDefault() method was used in an element?
We can use the event.defaultPrevented property in the event object. It returns a boolean indicating if the event.preventDefault() was called in a particular element.
5 .
What are the falsy values in JavaScript?
const falsyValues = ['', 0, null, undefined, NaN, false];
falsy values are values that when converted to boolean becomes false.
6 .
What is the prototype of an object?
A prototype in simplest terms is a blueprint of an object. It is used as a fallback for properties and methods if it does exist in the current object. It's the way to share properties and functionality between objects. It's the core concept around JavaScript's Prototypal Inheritance.

  const o = {};
  console.log(o.toString()); // logs [object Object] 
 
Even though the o.toString method does not exist in the o object it does not throw an error instead returns a string [object Object]. When a property does not exist in the object it looks into its prototype and if it still does not exist it looks into the prototype's prototype and so on until it finds a property with the same in the Prototype Chain. The end of the Prototype Chain is the Object.prototype.

   console.log(o.toString === Object.prototype.toString); // logs true
   // which means we we're looking up the Prototype Chain and it reached 
   // the Object.prototype and used the "toString" method.
7 .
What is the use Function.prototype.apply method?
The apply invokes a function specifying the this or the "owner" object of that function on that time of invocation.

const details = {
  message: 'Hello World!'
};

function getMessage(){
  return this.message;
}
getMessage.apply(details); // returns 'Hello World!'
 
 
This method works like Function.prototype.call the only difference is how we pass arguments. In apply we pass arguments as an array.

const person = {
  name: "Marko Polo"
};

function greeting(greetingMessage) {
  return `${greetingMessage} ${this.name}`;
}

greeting.apply(person, ['Hello']); // returns "Hello Marko Polo!"
8 .
What is the use Function.prototype.call method?
The call invokes a function specifying the this or the "owner" object of that function on that time of invocation.
const details = {
  message: 'Hello World!'
};

function getMessage(){
  return this.message;
}

getMessage.call(details); // returns 'Hello World!'

This method works like Function.prototype.apply the only difference is how we pass arguments. In call we pass directly the arguments separating them with a comma , for every argument.

const person = {
  name: "Marko Polo"
};

function greeting(greetingMessage) {
  return `${greetingMessage} ${this.name}`;
}

greeting.call(person, 'Hello'); // returns "Hello Marko Polo!"
9 .
What is console.log() and why it is used?
It is not a jQuery feature but a feature for debugging purposes used by developers. It is used to writes a message to the console.
 
console.log() accepts a parameter which can be an object, an array or any message.
 
Syntax : console.log(name); // here name is object, an array or variable.
 
Example :
                                                    
$('#form').submit(function() {

     console.log(‘Your form is submitted successfully!’);

     // do something

}); 
10 .
What is the importance of JavaScript?
It is a client side technology, it is mainly used for gives client side validation, but it have lot of features which are given below;
 
* It is an object-based scripting language.
* It helps the user more control over the browser.
* JavaScript Handling dates and time also.
* It also helps to Detect the client's browser and Operating System.
* JavaScript is light weighted & case sensitive.
* All most JavaScript control statements syntax is same as syntax of control statements in C programming language.