970_90

Monday, 6 June 2016

Core JavaScript Interview Questions

Core JavaScript Interview Questions

Question: How to call a function after page load?
window.onload = function() {
    consol.log('Page loaded successfully');
};



Question: How to detect a mobile device in JavaScript?
var isMobile = false; 
if( /Android|webOS|iPhone|iPod|iPad|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    isMobile=true;
}

if(isMobile){
    /** Write here code **/

    /** Write here code **/
}



Question: How to get all elements in having same class in JavaScript?
var element = document.getElementsByClassName("className");
for (index = element.length - 1; index >= 0; index--) {
    /** 
    Write your code here
    **/
}



Question: How to add a class to a given element?
var element = document.getElementById("myDivId");
d.className += " anotherClass";  //It will add anotherClass to the #myDivId



Question: How to update a class to a given element?
var element = document.getElementById("myDivId");
d.className = " anotherClass";  //anotherClass will added



Question: How to DELETE a class to a given element?
var element = document.getElementById("myDivId");
d.className = '';  //All exist class will be removed



Question: How to get element object by tag name?
var element = document.getElementsByTagName("h2");



Question: How do I remove a property from a JavaScript object?
var myJSONObject = {"Key1": "value1", "Key2": "value2", "Key3": "value3"};
delete myJSONObject.two;
console.log(myJSONObject); /* Object { Key1="value1",  Key2="value2",  Key3="value3"}*/



Question: How do I add a property from a JavaScript object?
var myJSONObject = {"Key1": "value1", "Key2": "value2", "Key3": "value3"};
myJSONObject.Key4='value4';
console.log(myJSONObject); /* Object { Key1="value1",  Key2="value2",  Key3="value3",  Key4="value4" }*/



Question: How do I update a property from a JavaScript object?
var myJSONObject = {"Key1": "value1", "Key2": "value2", "Key3": "value3", "Key4": "value4"};
myJSONObject.Key4='444444444444';
console.log(myJSONObject); /* Object { Key1="value1",  Key2="value2",  Key3="value3",  Key4="444444444444"}*/



Question: How do I count the size of object?
var myJSONObject = {"Key1": "value1", "Key2": "value2", "Key3": "value3"};
Object.keys(myJSONObject).length; //3



No comments:

Post a Comment