Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15K views

Javascript Interview Questions

Javascript is a scripting language used for client-side web development. It is based on the ECMAScript standard but includes additional features. Variables can be declared with var or implied globally without var. The main types in JavaScript are Number, String, Boolean, Function, Object, Null, and Undefined. The this keyword refers to the object that owns the method or event handler.

Uploaded by

Tyler
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15K views

Javascript Interview Questions

Javascript is a scripting language used for client-side web development. It is based on the ECMAScript standard but includes additional features. Variables can be declared with var or implied globally without var. The main types in JavaScript are Number, String, Boolean, Function, Object, Null, and Undefined. The this keyword refers to the object that owns the method or event handler.

Uploaded by

Tyler
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Javascript Interview Questions

What is JavaScript? 
Ans:JavaScript is a scripting language most often used for client-side web development.
2) What is the difference between JavaScript and Jscript? 
Ans:Both JavaScript and Jscript are almost similar. JavaScript was developed by Netscape. Microsoft reverse
engineered Javascript and called it JScript.
3) 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.
Is JavaScript case sensitive? 
Ans:Yes!
A function getElementById is not the same as getElementbyID.
5) What are the types used in JavaScript? 
Ans:String, Number, Boolean, Function, Object, Null, Undefined.
6) What are the boolean operators supported by JavaScript? And Operator: &&
Or Operator: ||
Not Operator: !
7) What is the difference between “==” and “===”? 
Ans:
“==” checks equality only, 
“===” checks for equality as well as the type.
8) How to access the value of a textbox using JavaScript? 
<input type="text" id="txtFullName"
name="FirstName" value="Vikas Ahlawat">

var name = document.getElementById('txtFullName').value;


var name = document.forms[0].FirstName.value;

This uses the "name" attribute of the element to locate it.

 How will you get the Checkbox status whether it is checked or not? 


Ans:

 Collapse | Copy Code

var status = document.getElementById('checkbox1').checked;


alert(status);

will return true or false.

11) How to create arrays in JavaScript? 


Ans:There are two ways to create array in JavaScript like other languages:

a) The first way to create array


Declare Array:

var names = new Array();


Add Elements in Array:-
names[0] = "Vikas";
names[1] = "Ashish";
names[2] = "Nikhil";

This is the second way:

var names = new Array("Vikas", "Ashish", "Nikhil");

What do you understand by this keyword in JavaScript? 


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):

What does "1"+2+4 evaluate to? 


Ans: Since 1 is a string, everything is a string, so the result is 124.
18) What does 3+4+"7" evaluate to? 
Ans: Since 3 and 4 are integers, this is number arithmetic, since 7 is a string, it is concatenation, so 77 is the
result.
19) How do you change the style/class on any element using JavaScript? 
document.getElementById(“myText”).style.fontSize = “10";

 What is an object in JavaScript, give an example? 


Ans: An object is just a container for a collection of named values:

var man = new Object();


man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;

What is the similarity between the 1 st and 2nd statement? 


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

Consider the following statements and tell what would be the output of the logs statements?

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. */

What is this?

var myArray = [[[]]];

Name any two JavaScript functions which are used to convert nonnumeric values into numbers? 

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

Does JavaScript Support automatic type conversion, If yes give example.

Ans: Yes! Javascript support automatic type conversion. You should take advantage of it, It is most common way of
type conversion used by Javascript developers.

Ex.

var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"

Javascript on the Server Side – Node.js


Javascript can also be run by the server. Node.js is a perfect example of how Javascript can be run on the server
side – not the client side.

Node.js is a combination of a runtime environment and a library that uses server side Javascript to build web based
applications. Again, the key here is that it is server side Javascript, which means that Javascript is executed by the
server – not the browser. Node.js also happens to use the V8 virtual machine, which is the Javascript virtual
machine that is used in Google’s Chrome web browser.

What is ECMAScript?
ECMAScript is a standard for a scripting language, and the Javascript language is based on the ECMAScript
standard.

Is Javascript exactly the same as ECMAScript?


No, Javascript is not exactly equivalent to ECMAScript. The core features of Javascript are based on the ECMAScript
standard, but Javascript also has other additional features that are not in the ECMA specifications/standard.

Var versus no Var


The “var” keyword in front of a variable will basically declare that variable within the current scope. But, if that “var”
keyword is missing, Javascript will search up the scope chain to see if there is a variable with that name in a
different scope. And, since it finds a variable with the same name – “testVar” – in the global scope, Javascript will
use that variable instead of declaring a new one local to the function.

What happens when you don’t declare a variable in Javascript?


As we showed in the code above, by not declaring the variable testVar in the code above we were accessing a global
variable. If we did not already have that global variable declared earlier, then we would be creating what’s called
an implied global variable. It’s highly recommended that you always declare your variables in Javascript – even
though Javascript does not require that you do.

JSON
JSON (JavaScript Object Notation) is a standard that is specifically meant for data interchange, and because JSON is
a subset of Javascript, you should be able to use the function above without needing any supporting library files.
The stringify method simply converts the Javascript object into JSON text, which is something that’s very easy to
read and print out to the page.

What are JavaScript types?


Unlike Java or C#, JavaScript is a loosely-typed language (some call this weakly typed); this means that
no type declarations are required when variables are created. Strings and numbers can be intermixed
with no worries. JavaScript is smart, so it easily determines what the type should be. The types
supported in JavaScript are: Number, String, Boolean, Function, Object, Null, and Undefined.

What is the difference between undefined and null?


The value of a variable with no value is undefined (i.e., it has not been initialized). Variables can be
emptied by setting their value to null. You can test for each using the === (three equal signs) or == (two
equal signs) for comparison checking.
What is JavaScript's this keyword?
JavaScript's this keyword normally refers to the object that owns the method, but it depends on how a
function is called. Basically, it points to the currently in scope object that owns where you are in the
code. When working within a Web page, this usually refers to the Window object. If you are in an object
created with the new keyword, the this keyword refers to the object being created. When working with
event handlers, JavaScript's this keyword will point to the object that generated the event.

You might also like