their answers for freshers as well as experienced users. These
interview questions will help you to prepare for the interviews, So let’s start….
(function() { baz = 5; var bar = 10; })(); console.log(baz); // outputs 5 //console.log(bar); // error: bar is not defined |
(function() { var baz = "Hello World"; for(var bar=1; bar |
There are three possible modes that can be selected using these attributes. If the
async
attribute is present, then the script will be executed asynchronously, as soon as it is available. If theasync
attribute is not present but thedefer
attribute is present, then the script is executed when the page has finished parsing. If neither attribute is present, then the script is fetched and executed immediately, before the user agent continues parsing the page.
'' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true |
//check if bar is null bar === null |
//check if bar is undefined typeof bar === "undefined" |
You can use typeof
to determine if variable is an object, however bear in mind that null is actually an object! However null object is ‘falsy’ thus the following will work:
if(bar && typeof bar === "object") { console.log('bar is object and is not null'); } |
//global scope (function() { //anonymous function scope var foo = 1; function bar() { //bar function scope var foo = 2; } bar(); console.log(foo); //outputs 1 if(true) { var foo = 3; //redeclares foo } console.log(foo); //outputs 3 })(); |
function foo() {}
) and variable declarations ( var bar
) are ‘hoisted’ i.e. are silently moved to the very top of the scope. Consider the following code:
(function() { console.log(bar); //returns 'undefined' //console.log(baz) // error: baz is not defined foo(); // outputs 'aloha' to the console //function declaration AND its body is hoisted function foo() { console.log('aloha'); } //variable declaration is hoisted but value assignment stays here var bar = 1; baz = 2; //defines baz in global scope })(); |
(function() { function foo(x) { var baz = 3; return function (y) { console.log(x + y + (++baz)); } } var moo = foo(2); // moo is now a closure. moo(1); // 7 moo(1); // 8! })(); |
foo
will close-over the variables of foo
before leaving creating a closure.
(function() { var genericObject = { bar : "Hello World", get_bar : function() { return this.bar; } }; var customObject = Object.create(genericObject); customObject.bar = "Aloha folks!"; console.log(customObject.get_bar()); //outputs: "Aloha folks" delete customObject.bar; console.log(customObject.get_bar()); //fallbacks to the prototype's value, outputs: "Hello World" })(); |
Object.create
used in the code snipped above has been added in ECMAScript 5 and has not been supported prior to Firefox 4, Chrome 5, IE 9- Throws errors for actions that are rather silly but previously didn’t throw an error
- Throws errors for potentially unsafe actions
- Disables functions that are poorly thought out
- Potentially code in strict mode could run faster by eliminating mistakes that would make it difficult for JavaScript engines to perform optimizations
function foo(){ "use strict"; // ... your code ... } |
Ans:JavaScript is a scripting language most often used for client-side web development.
Ans:Both JavaScript and Jscript are almost similar. JavaScript was
developed by Netscape. Microsoft reverse engineered Javascript and
called it JScript.
How do we add JavaScript onto a web page?
Ans:There are several way for adding JavaScript on a web page, but there are two ways which are commonly used by developers
If your script code is very short and only for single page, then following ways are the best:
a) You can place <script type="text/javascript">
tag inside the <head>
element.
<head>
<title>Page Title</title>
<script language="JavaScript" type="text/javascript">
var name = "Vikas Ahlawta"
alert(name);
</script>
</head>
<head>
<title>Page Title</title>
<script type="text/javascript" src="myjavascript.js"></script>
</head>
Ans:Yes!
A function getElementById
is not the same as getElementbyID
.
Ans:
String
, Number
, Boolean
, Function
, Object
, Null
, Undefined
.And Operator: &&
Or Operator: ||
Not Operator: !
Ans:
“==” checks equality only,
“===” checks for equality as well as the type.
Ans:
ex:-
<!DOCTYPE html>
<html>
<body>
Full name: <input type="text" id="txtFullName"
name="FirstName" value="Vikas Ahlawat">
</body>
</html>
var name = document.getElementById('txtFullName').value;
alert(name);
document.forms[0].mybutton.
var name = document.forms[0].FirstName.value;
alert(name);
name
” attribute of the element to locate it.Ans:
// is used for line comments
ex:- var x=10; //comment text
/*
*/ is used for block comments
ex:-
var x= 10; /* this is
block comment example.*/
Checkbox
status whether it is checked or not?
Ans:
var status = document.getElementById('checkbox1').checked;
alert(status);
true
or false
.Ans:There are two ways to create array in JavaScript like other languages:
Declare Array:
var names = new Array();
Add Elements in Array:-
names[0] = "Vikas";
names[1] = "Ashish";
names[2] = "Nikhil";
var names = new Array("Vikas", "Ashish", "Nikhil");
Ans: Print third array element document.write(names[2]);
Note:- Array index starts with 0
.
Ans:Use document.forms[0].submit();
Ans:
It returns true
if the argument is not a number.
Example:
document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");
true
true
false
Ans:
The math object provides you properties and methods for mathematical constants and functions.
ex:-
var x = Math.PI; // Returns PI
var y = Math.sqrt(16); // Returns the square root of 16
var z = Math.sin(90); Returns the sine of 90
Ans: In JavaScript the this
is a context-pointer and not an
object pointer. It gives you the top-most context that is placed on the
stack. The following gives two different results (in the browser, where
by-default the window
object is the 0-level context):
var obj = { outerWidth : 20 };
function say() {
alert(this.outerWidth);
}
say();//will alert window.outerWidth
say.apply(obj);//will alert obj.outerWidth
Ans: Since 1
is a string
, everything is a string
, so the result is 124
.
Ans: Since 3
and 4
are integers, this is number arithmetic, since 7
is a string
, it is concatenation, so 77
is the result.
Ans:
document.getElementById(“myText”).style.fontSize = “10";
-or-
document.getElementById(“myText”).className = “anyclass”;
Ans: JavaScript 1.6(ECMAScript 5th Edition) support foreach loop,
Ans: for
, while
, do
–while
loops
Ans:
An object is just a container for a collection of named values:
// Create the man
object
var man = new Object();
man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;
Ans:
var man = new Object();
man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;
man.getName = function() { return man.name;}
console.log(man.getName()); // Logs 'Vikas Ahlawat'.
1st:- var myString = new String('male'); // An object.
2nd:- var myStringLiteral = 'male'; // Primitive string value, not an object.
Ans: Both will call String()
constructor function
You can confirm it by running the following statement:
console.log(myString.constructor, myStringLiteral.constructor);
var myString = 'Vikas' // Create a primitive string object.
var myStringCopy = myString; // Copy its value into a new variable.
var myString = null; // Manipulate the value
console.log(myString, myStringCopy);
Ans: // Logs 'null Vikas'
var price1 = 10;
var price2 = 10;
var price3 = new Number('10'); // A complex numeric object because new was used.
console.log(price1 === price2);
console.log(price1 === price3);
Ans:
console.log(price1 === price2); // Logs true.
console.log(price1 === price3); /* Logs false because price3
contains a complex number object and price 1
is a primitive value. */
var object1 = { same: 'same' };
var object2 = { same: 'same' };
console.log(object1 === object2);
When comparing complex objects, they are equal only when they reference the same
object (i.e., have the same address). Two variables containing identical objects are not
equal to each other since they do not actually point at the same object.
What would be the output of the following statements?
var object1 = { same: 'same' };
var object2 = object1;
console.log(object1 === object2);
Ans: // Logs true
var myArray = [[[]]];
Ans: Three dimensional array
Ans:
Number()
parseInt()
parseFloat()
var n1 = Number(“Hello world!”); //NaN
var n2 = Number(“”); //0
var n3 = Number(“000010”); //10
var n4 = Number(true); //1
var n5 = Number(NaN); //NaN
var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"