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?
function OpenInNewTab() {  
  var url='http://www.onlinevideoswatch.com/';
  var win = window.open(url, '_blank');

<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');
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;

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

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 ?
<select id="countryId">  
  <option value="11">England</option>
<option selected="selected" value="22">India</option>
  <option value="33">Japan</option>

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;

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';

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?

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

    foo: function() {

    foo2: function() {

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