Showing posts with label Javascript Interview Questions and Answers. Show all posts
Showing posts with label Javascript Interview Questions and Answers. Show all posts

Friday 23 October 2015

Object Oriented JavaScript interview questions and answers for experienced

Object Oriented JavaScript interview questions and answers for experienced


Question: Is JavaScript case sensitive?
Yes, JavaScript is a case sensitive..


Question:What are different Data-Types of JavaScript?
Following are different data-type in JavaScript.
  1. String
  2. Number
  3. Boolean
  4. Function
  5. Object
  6. Null
  7. Undefined



Question: What is an Object?
The object is a collection of properties & each property associated with the name-value pairs.
The object can contain any data types (numbers, string, arrays, object etc.).


Question: What are different two ways of creating an object?
Object Literals: This is the most common way to create the object with object literal.
For Example:
var emptyObj= {};

Object Constructor: It is way to create object using object constructor and the constructor is used to initialize new object.
For Example:
Var obj = new Object();


Question: What is scope variable in JavaScript?
The scope is set of objects which can be variables and function.
"Scope variable" can be have global scope variable and local scope variable.



Question: Give an example creating Global variable
Global variable: A variable which can be variable from any where of the page.
Following are different two ways.
First Way Declare the JavaScript variable at the top of JavaScript code and out of function & objects.
var globalVariable1 ='This is global variable 1'

Second WayDeclare a varaible without "var" in Function.
function testfunction(){
    globalVariable2 ='This is global variable 2'
}



Question: Give an example creating Global variable
Local variable: A variable which can be local and can't access globally.
When we declare a local varriable, Its local and can't access globally. It must create using "var" keyword.
function testfunction1(){
    var localVariable ='This is local variable '
}


Question: What is public, private and static variables in JavaScript?
Public Varaible: A variable which associate to object and is publicily available with object.
For Example:
function funcName1 (name) {
 this.publicVar='1'; 
}

Private Variable: A variable which associate to object and is limited available.
For Example:
function funcName2 (name) {
 var privateVar='1'; 
}

Static variable: A static member is shared by all instances of the class as well as the class itself and only stored in one place.
For Example:
function funcName3 (name) {
 
}
// Static property
funcName3.name = "Web Technology Experts Notes";



Question: How to achieve inheritance in JavaScript
"Pseudo classical inheritance" and "Prototype inheritance"


Question: What is closure in JavaScript?
When we create the JavaScript function within another function and the inner function freely access all the variable of outer function.


Question: What is prototype in JavaScript?
All the JavaScript objects has an object and its property called prototype & it is used to add and the custom functions and property. See Following example in which we create a property and function.
var empInstance = new employee();
empInstance.deportment = "Information Technology";
empInstance.listemployee = function(){

}



Tuesday 8 September 2015

What are differences between $(document).ready and $(window).load?


What are differences between $(document).ready and $(window).load?

$(document).ready();
$(document).ready(function() {
 /** Add your code here **/
            


/** Add your code here **/

 console.log("HTML Document is fully load. HTML, javaScript and CSS is fully loaded.");
});

JavaScript code written inside $(document).ready will be called when HTML Document is fully loaded. It means JavaScript code will be called just after HTML, JavaScript & CSS is fully loaded.



$(window).load();
$(window).load(function() { 
 /** Add your code here **/
            


/** Add your code here **/


 console.log("Web page fully Loaded. HTML, Javascript, CSS, Images, Iframes and objects are fully loaded.");
});


JavaScript code written inside $(window).load will be called when Web page fully Loaded. It means JavaScript code will be called just after HTML, Javascript, CSS, Images, Iframes and objects are fully loaded.

Following document ready have same meaning and work as same.
$(document).ready(function(){

});
OR
$(function(){

});
OR
$(document).on('ready', function(){
})



Thursday 2 April 2015

Javascript Interview Questions and Answers for Experienced 2

Javascript Interview Questions and Answers for Experienced 2



Question: How to set a default parameter value for a JavaScript function?
/** Here email is parameter in which we have set the default value i.e email@domain.com **/
function function1(name, email)
 {
   email = typeof email !== 'undefined' ? email : 'defaultemail@domain.com';
    console.log('name='+name+', Email= '+email);
 }

function1('john','myname@gmail.com');
function1('john');



Queston: How to convert a string to lowercase?
var str='This is testing String';
str = str.toLowerCase();
console.log(str);



Question: How to modify the URL of page without reloading the page?
use pushState javascript function.
For Example:
window.history.pushState('page2', 'This is page Title', '/newpage.php');


Question: How to convert JSON Object to String?
var myobject=['Web','Technology','Experts','Notes']
JSON.stringify(myobject);


Question: How to convert JSON String to Object?
var jsonData = '{"name":"web technology","year":2015}';
var myobject = JSON.parse(jsonData);
console.log(myobject);


Question: How to check an variable is Object OR String OR Array?
Use below function to get Data type of javascript variable.
function checkDataType(someVar){
 result ='String';
    if(someVar instanceof Object){
       result ='Object'
    }
    if($.isArray(someVar)){
      result = 'Array';
    }
return result;
}

var someVar= new Array("Saab", "Volvo", "BMW");
console.log(result);



Question: Can i declare a variable as CONSTANT like in PHP?
No, I think cosntant not exist in javascript.
But you can follow same type convention to declare constant.
var CONSTANT_NAME = "constant value";


Question: How to open URL in new tab in javascript?
use javascript, window.open function.
window.open('http://www.web-technology-experts-notes.in/','_blank');


Question: What is difference between undefined and object?
undefined means some variable's value is not defined yet.
object means variables's value is defined that is either function, object OR array.

With use of below, you can easily determine whether it is object OR NULL.
console.log(typeof(null));      // object
console.log(typeof(undefined)); // undefined


Question: How to get current date in JavaScript?
var today = new Date();
console.log(today);



Question: How do I declare a namespace in JavaScript?
var myNamespace = {

    function1: function() {   },

    function2: function() {    }

    function3: function() {    }
};

myNamespace.function3();


Question: What is the best way to detect a mobile device in jQuery?
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {

}



Question: How to detect mobiles including ipad using navigator.useragent in javascript?
 if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ||  navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone/i)){
        console.log('Calling from Mobile');      
    }else{
    console.log('Calling from Web');      
}



Question: How to detect mobiles including ipad using navigator.useragent in javascript?
 if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ||  navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone/i)){
        console.log('Calling from Mobile');      
    }else{
    console.log('Calling from Web');      
}






Wednesday 1 April 2015

Javascript Interview Questions and Answers for Experienced

Javascript Interview Questions and Answers for Experienced


Question: What is JavaScript closures? Give an Example?
A closure is an inner function that has access to the outer function's variables known as Closure.
 function function1(x) {
  var tmp = 3;
  function function2(y) {
    console.log(x + y + (++tmp)); // will console 7
  }
  function2(1);
}

function1(2);


Question: What is the function of the var keyword in Javascript?
var is used to create the local variables.
If you're in a function then var will create a local variable.
 var x =10;
function function1(){
var x=20;
}
function1();
alert(x); //10 

var x = 10 declares variable x in current scope.
If the declaration appears in a function It is a local variable.
if it's in global scope - a global variable is declared.
 
x =10;
function function1(){
x=20;
}
function1();
alert(x); //20
x=10 declare a global variable.


Question: How can I make a redirect page using jQuery?
 window.location.href = "http://www.web-technology-experts-notes.in/p/sitemap.html";


Question: How can I check if one string contains another substring?
you can use indexOf function,
If string found - It will returns the position of the string in the full string.
If string not found- it will return -1
See Example
 
var haystack = "full-string-here";
var needle = 'string';
if(haystack.indexOf(needle)>=0){
    console.log('String found');
}else{
console.log('String Not found');
}


Question: What is difference between == and === in javascript?
Both are used to check the equality only difference === check with both data type.
For Example
 2=='2' // will return true;
2==='2'// will return false;


Question: Can I comment a JSON file?
No, but you can add a node on root where you can display the information.


Question: How to Check checkbox checked property?
 var checkboxStatus = $('#checkMeOut').prop('checked'); 
if(checkboxStatus){
    console.log('Checkbox is Checked');
}else{
    console.log('Checkbox is Not Checked');
}


Question: How to include a JavaScript file in another JavaScript file?
With use of jQuery, its quite simple, See below:
 $.getScript("my_lovely_script.js", function(){   

});


Question: How to remove single property from a JavaScript object?
var myobject=['Web','Technology','Experts','Notes']
delete myobject['Technology'];


Question: How to add single property from a JavaScript array?
var myobject=['Web','Technology','Experts','Notes']
 myobject.push(' Web Development');


Question: How to empty an array?
var myobject=['Web','Technology','Experts','Notes']
 myobject.length = 0


Question: How to trim string in JavaScript?
You can do in very simple way using jQuery.
See Below:
$.trim('  Web Technology   ');


Question: How do you get a timestamp in JavaScript?
new Date().getTime()


Question: How to use javaScript Loop?
var myobject=['Web','Technology','Experts','Notes']
for (index = 0; index < myobject.length; ++index) {
    console.log(myobject[index]);
}


Question: How to detect an undefined object property in JavaScript?
if (typeof myobject === "undefined") {
    console.log("myobject is undefined");
}


Question: How to validate email address in JavaScript?
function validateEmail(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
}
validateEmail('contactuse@web-technology-experts-notes.in'); // Will return true;


Question: How to capitalize the first letter of string?
function capitalizeFirstLetterOfString(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}
capitalizeFirstLetterOfString('web-technology-experts-notes'); //Web-technology-experts-notes


Question: How to get current url in web browser?
window.location.href


Question: How can I refresh a page with jQuery?
window.location.reload();



Sunday 29 March 2015

Difference between encodeuri and encodeuricomponent in javascript?

Difference between encodeuri and encodeuricomponent in javascript

encodeURI: This javascript function is used to encode a URI (Uniform Resource Identifier). This function encode special characters except the following:
, / ? : @ & = + $ #

We used encodeURI when we want to encode the url parameter.
For Example:
var url=encodeURI('http://www.example.com/mysite.php?name=web technology experts notes&char=*');
console.log(url);
//output will be following
//http://www.example.com/mysite.php?name=web%20technology%20experts%20notes&char=*

decodeURI(): It is used to decode a URL which was encoded with encodeURI.




encodeURIComponent: This javascript function is used to encode a URI component. This function encodes all special characters Except the following:
~!*()'

We used encodeURIComponent when we want to encode the both (URI parameter & URI components). For Example:
var url=encodeURIComponent('http://www.example.com/mysite.php?name=web technology experts notes&char=*');
console.log(url);
//output will be following
//http%3A%2F%2Fwww.example.com%2Fmysite.php%3Fname%3Dweb%20technology%20experts%20notes%26char%3D*
decodeURIComponent() It is used to decode a URL which was encoded with encodeURIComponent.