Wednesday, 6 April 2016

Javascript Questions And Answers for Fresher and Experienced

Javascript Questions And Answers for Fresher and Experienced

Question: How to pass multiple parameter in setTimeout function?
function myFunctionName(var1,var2){
    console.log('called after 2 sec of page load '+var1+' '+var2);

Question: How to enumerate the properties of js objects?
var myObject = {name1: 'Value1',name2: 'Value2'};
//console.log(myObject); //It will print all the values

for (var name in myObject) {

Question: How to measure the execution time of javascript script?
var startTime = new Date().getTime();
Write here you script
for(i=1;i<=500000; i++){
var endTime = new Date().getTime();

var time = endTime - startTime;
console.log('Execution time in Milli Seconds: ' + time);
console.log('Execution time in Seconds: ' + time/1000);

Question: How to listen (Do some changes) the window.location.hash change?
$(window).on('hashchange', function() {  
function callNewFunction(url){
    console.log('Hash URL is called');

After appling above code, whenever you add/update the hash value, callNewFunction will called automatically.

hashchange event is HTML5 feature and supported by all modern browsers and support is added in following browser.
  1. Internet Explorer 8
  2. Firefox 3.6
  3. Chrome 5
  4. Safari 5
  5. Opera 10.6

Question: How to add class in an element?
<div id="myDivId">

javaScript Part
var d = document.getElementById("myDivId");
d.className += " newClass";

Question: How to get the list of classes for an element?
d = document.getElementById("myDivId");

Question: Can we compare two javaScript objects?
Yes, We can compare two javascript objects. See Following examples.
var myObject1 = {name1: 'Value1',name2: 'Value2'};
var myObject2 = {name1: 'Value111',name2: 'Value222'};
if(JSON.stringify(myObject1) === JSON.stringify(myObject2)){
 console.log("Both object are same");
console.log("Both object are different");

Question: What is difference between Array(3) and Array('3') in javascript?
new Array(3), means declare the 3 elements and each have value "undefined". new Array('3'), means declare the 1 element and have value 3.
console.log(new Array(3)); // [undefined, undefined, undefined]
console.log(new Array('3')); // ["3"]

Question: How to get browser URL for all browser?

Question: How to remove an element(string OR object ) from javascript Array/Object?
var myObject = {name1: 'Value1',name2: 'Value2'};
delete myObject['name1']

Question: What does jQuery.fn mean?
jQuery.fn is just an prototype for defining the new functions.
fn is an alias to the prototype property.
For Example,
$.fn.newFunctionName = function(val){
console.log('something '+val); //something Test Value
$.fn.newFunctionName('Test Value');

Question: How to remove an empty elements from an Array?
var simpleArray = [1,3,,3,null,,0,,undefined,4,,6,,];
var cleanArray = simpleArray.filter(function(n){ return n != undefined }); 

Question: How to add 5 days in JavaScript?
var result = new Date();
result.setDate(result.getDate() + 5);