Sei sulla pagina 1di 4

Interview questions

JavaScript

1) What is JavaScript?
JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented
language.

2) What is variable scoping?
A variable can be either of global or local scope. A global variable is a variable
declared in the main body of the source code, outside all functions, while a local
variable is one declared within the body of a function or a block.

3) What is a closure?
The function keyword within another function, the inner function has access to
variables in the outer function.
Example:
function foo(x) {
var tmp = 3;
return function (y) {
alert(x + y + (tmp)); // will also alert 16
}
}
var bar = foo(2); // bar is now a closure.
bar(10);

4) What does variable hoisting mean in JavaScript?
In JavaScript, you can have multiple var-statements in a function. All of these
statements act as if they were declared at the top of the function. Hoisting is the act
of moving the declarations to the top of the function.

5) What is the difference between == and === in JavaScript?
== checks equality whereas === checks equality and type.

6) What kind of conditional statements are available in JavaScript?
IF statement
IF ELSE statement
IF ELSE if statement
SWITCH statement

7) What kind of loops are available in JavaScript?
FOR loop -
FOR IN loop
WHILE loop
DO while - This does one full iteration before checking the condition

8) Explain how this works in JavaScript?
Unlike other object oriented languages "this" does not necessarily relate to the
surrounding object, it is the current context of the executing code. Call and apply can
be used to change the meaning of "this".

9) Explain what is Prototype in JavaScript?
A prototype is an internal object from which other objects inherit properties. Its
main purpose is to allow multiple instances of an object to share a common
property.

10) How would you change the context of the function
By using call and apply method - These two simple methods inherent to all
functions allow you to execute any function in any desired context. The call
function requires the arguments to be listed explicitly while the apply function
allows you to provide the arguments as an array.


11) How do you determine if a JavaScript instance object was created from a specific
constructor?
By using instanceof operator it Checks the current object and returns true if the
object is of the specified object type.

12) What are the different JavaScript data types? Give example of each.
Undefined
Number
String
Boolean
Object
Function
Null

13) What is the difference between a value that is undefined and a value that is null?
Null is actually an assignment value, and is used to attribute an empty value to a
variable.
A variable is undefined when its been declared without an assigned value.

14) Explain how timers work in JavaScript.

setTimeout(function, milliseconds): Creates a timer that will call a function after a
designated amount of milliseconds. This also generates an id value so the coder can
access it another time.

setInterval(function, milliseconds): Acts the same way as the setTimer function, only
it repeats itself based on the number of milliseconds given.

clearInterval(id): Used to stop a timer.

15) Is JavaScript case sensitive?
Yes, absolutely. For example, the function getElementById is not the same as the
function getElementbyID. Keeping your capitalization consistent is important.

16) Explain the different types of pop-up boxes you can create in JavaScript.

Alert Box: Used to confirm that a user understands a vital piece of information
before proceeding. The user must click OK to exit the box.

Confirm box: Used when user verification is required. It will return TRUE if the user
clicks OK, and FALSE if the user clicks CANCEL.

Prompt box: Used if the user needs to input something before proceeding. When
the user inputs a value and presses OK, the prompt box will return the input value. If
the user clicks CANCEL without inputting a value, the input value will return as null.

17) What is an Object Literal?
An object literal is a comma-separated list of name-value pairs wrapped in curly
braces. Object literals encapsulate data, enclosing it in a tidy package. This minimizes
the use of global variables which can cause problems when combining code.

Object literal property values can be of any data type, including array literals,
functions, and nested object literals.

18) What is a Promise in JavaScript?
A promise represents the eventual value returned from the single completion of an
operation. A promise may be in one of the three states, unfulfilled, fulfilled, and
failed. The promise may only move from unfulfilled to fulfilled or unfulfilled to failed.

19) What is event bubbling and event capturing?
Event bubbling and capturing are two ways of event propagation in HTML DOM.

In bubbling the event is first captured and handled by the inner most element and
then propagated to outer elements.

In capturing the event is first captured by the outer most element and propagated to
the inner most element.

20) Explain how to handle errors in JavaScript.
You can handle runtime exceptions by using try and catch statements. Run the code
you want to test in the try block, and if a runtime error occurs, it will execute the
code designated in the catch block.

Potrebbero piacerti anche