Monday 4 April 2016

Javascript Interview Questions And Answers for 3 year experienced

Javascript Interview Questions And Answers for 3 year experienced

Question: How to open a URL in a new tab using javaScript?
JavaScript
function OpenInNewTab() {  
  var url='http://www.onlinevideoswatch.com/';
  var win = window.open(url, '_blank');
  win.focus();
}

Html
<div onclick="OpenInNewTab();">
Click to Open in Tab</div>



Question: How to Convert JavaScript String to be all lower case?
var strVal="How are yOu";
strVal.toLowerCase(); //how are you



Question: How to detect a mobile device in javaScript?
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 /** You are using Mobile Devices**/
}



Question: How do you check if a variable is an array in JavaScript?
var myArray=new Array('one','two','three','four');
console.log(myArray);
var myval='two';

if (myArray.indexOf(myval)) {
  console.log('value is Array!');
} else {
  console.log('Not an array');
}



Question: How to generate random number between two numbers (1-100)?
var minimum=1
var maximum=100
var randomnumber 

randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(randomnumber);



Question: How to declare constant in javaScript?
const MY_CONSTANT_NAME = "value that will never change";

Here you can't change the value of "MY_CONSTANT_NAME"
It Will work in all browsers except IE 8, 9 and 10.


Question: How can I format numbers as money format?
var monthlyExpanse=98765.432
monthlyExpanse.toFixed(4);//98765.4320 
monthlyExpanse.toFixed(3);//98765.432 
monthlyExpanse.toFixed(2);//98765.43
monthlyExpanse.toFixed(1);//98765.4



Question: How can display JSON data in readble form?
var str = JSON.stringify(obj, null, 2); // space level  2



Question: How to Get selected value from dropdown list ?
HTML
<select id="countryId">  
  <option value="11">England</option>
<option selected="selected" value="22">India</option>
  <option value="33">Japan</option>
</select>

JavaScript
var e = document.getElementById("countryId");
var countryId = e.options[e.selectedIndex].value;
//console.log(countryId); //22



Question: How to convert a number to hexadecimal,octal ?
var myNumber= 500;
//console.log(myNumber.toString(16));//1f4
//console.log(myNumber.toString(8));//764



Question: How to convert a hexadecimal,octal number to decimal?
var hexaDecimal='1f4';
var octal='764';
console.log(parseInt(hexaDecimal,16)); //500
console.log(parseInt(octal,8)); //500



Question: How to convert a String to Decimal?
var strVal='100';
number=parseInt(strVal);



Question: What is use of instanceof?
var simpleStr = "This is string"; 
var myString  = new String();
var newStr    = new String("constructor");
var myDate    = new Date();
var myObj     = {};

simpleStr instanceof String; // returns false, checks the prototype chain, finds undefined
myString  instanceof String; // returns true
newStr    instanceof String; // returns true
myString  instanceof Object; // returns true



Question: How to Scroll to the top of the page?
var xCoord=200;
var yCoord=500;
window.scrollTo(xCoord, yCoord);



Question: How to display javaScript Object?
console.log(obj);



Question: How to use namespaces in javaScript?
var myNamespaceName = {

    foo: function() {
        alert('foo');
    },

    foo2: function() {
        alert('foo2');
    }
};

myNamespaceName.foo();//alert foo
myNamespaceName.foo2();//alert foo2