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


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;
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(){
alert(x); //20
x=10 declare a global variable.

Question: How can I make a redirect page using jQuery?
 window.location.href = "";

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';
    console.log('String found');
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'); 
    console.log('Checkbox is Checked');
    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) {

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(''); // 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?

Question: How can I refresh a page with jQuery?