10 Javascript Questions And Answers For Fresher And Experienced

Javascript Questions And Answers For Fresher And Experienced

10 Javascript Questions And Answers For Fresher And Experienced


What is HTML ?

HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages. Along with CSS, and JavaScript, HTML is a cornerstone technology, used by most websites to create visually engaging webpages, user interfaces for web applications, and user interfaces for many mobile applications.Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.


What is javascript ?

JavaScript is a lightweight, interpreted programming language. It is designed for creating network-centric applications. It is complimentary to and integrated with Java. JavaScript is very easy to implement because it is integrated with HTML. It is open and cross-platform.



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



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) {
  //console.log(name+'=>'+myObject[name]);
  }



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() {  
  callNewFunction(window.location.href)  
});
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?
HTML Part
 

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");
console.log(d.className);



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");
}else{
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?
console.log(window.location.href);



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



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 }); 
console.log(cleanArray);



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


Related Courses:

Angular JS Interview Questions and Answers

HTML Interview Questions and Answers

JQuery Interview Questions and Answers


Post a Comment