Docsity

JavaScript Interview Questions and Answers: Comprehensive Guide, Exams of Advanced Education

Toggle sidebar
bg1
JAVASCRIPT INTERVIEW QUESTIONS
AND ANSWERS
Explain how "this" works - Correct Answers -The "this" keyword acts as a reference to
the context object. The value of "this" changes, depending on what context you're
executing in.
Cases:
- so you can access its properties through dot notation.
- When you use call and apply, you get to decide what 'this' is bound to
- When you use the keyword 'new' to create an instance, the keyword 'this' is bound to
the instance not the function.
string manipulation - Correct Answers -http://www.sitepoint.com/15-javascript-string-
functions/
http://overapi.com/javascript/
charAt()
charCodeAt()
concat()
fromCharCode()
indexOf()
lastIndexOf()
match()
replace()
search()
slice()
split()
substr()
substring()
toLowerCase()
toUpperCase()
valueOf()
array manipulation - Correct Answers -http://overapi.com/javascript/
concat()
indexOf()
pf3
pf4
pf5
pf8

Partial preview of the text

Download JavaScript Interview Questions and Answers: Comprehensive Guide and more Exams Advanced Education in PDF only on Docsity!

JAVASCRIPT INTERVIEW QUESTIONS

AND ANSWERS

Explain how "this" works - Correct Answers -The "this" keyword acts as a reference to the context object. The value of "this" changes, depending on what context you're executing in. Cases:

  • so you can access its properties through dot notation.
  • When you use call and apply, you get to decide what 'this' is bound to
  • When you use the keyword 'new' to create an instance, the keyword 'this' is bound to the instance not the function. string manipulation - Correct Answers -http://www.sitepoint.com/15-javascript-string- functions/ http://overapi.com/javascript/ charAt() charCodeAt() concat() fromCharCode() indexOf() lastIndexOf() match() replace() search() slice() split() substr() substring() toLowerCase() toUpperCase() valueOf() array manipulation - Correct Answers -http://overapi.com/javascript/ concat() indexOf()

join() lastIndexOf() pop() push() reverse() shift() slice() sort() splice() toString() unshift() valueOf() conversion - Correct Answers -String.parseInt() returns int String.toUpperCase() ret string String.toLowerCase() ret string String.split() returns array Big O notation - Correct Answers -https://rob-bell.net/2009/06/a-beginners-guide-to-big- o-notation/ http://bigocheatsheet.com/ For an algorithm to have logarithmic efficiency, written O(log n), it must, for each element acted on, eliminate some fraction of the remaining inputs. Explain how prototypal inheritance works - Correct Answers -objects inherit from objects search up prototype chain until it finds a match 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. Thus, object properties which are defined using the prototype object are inherited by all instances which reference it. http://www.htmlgoodies.com/html5/tutorials/javascript-prototypical-inheritance- explained.html#fbid=RraRpHf2mNM http://stackoverflow.com/questions/2064731/good-example-of-javascripts-prototype- based-inheritance Since javascript doesn't have a concept of a class, we implement inheritance by creating an object which can act as a prototype for other objects.

A hash table is made up of two parts: an array (the actual table where the data to be searched is stored) and a mapping function, known as a hash function. The hash function is a mapping from the input space to the integer space that defines the indices of the array. In other words, the hash function provides a way for assigning numbers to the input data such that the data can then be stored at the array index corresponding to the assigned number. What is a linked list? - Correct Answers -a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. what is an array? - Correct Answers -Array data structure, an arrangement of items at equally spaced addresses in computer memory. Array data type, used in a programming language to specify a variable that can be indexed. Associative array, an abstract data structure model that generalizes arrays to arbitrary indices individually. Traditionally an array reserves a continuous allocation of memory of predefined length. In JavaScript this is not the case. A JavaScript array is simply a glorified object with a unique constructor and literal syntax and an additional set of properties and methods inherited from Array.prototype. If this means we make a small sacrifice in performance, it is more than compensated for by its ease of use and the power of its utilities. Unlike its counterparts in certain other languages, JavaScript arrays are a joy to use - this is something they definitely got right. What is IIFE? - Correct Answers -Immediately-Invoking Function Expression (function(){})(); //executes function immediately Explain why the following doesn't work as an IIFE: function foo(){ }(); - Correct Answers -When the parser encounters the 'function' keyword in the global scope or inside a function, it treats it as a function declaration (statement), and not as a function expression, by default. You can fix it by wrapping function in parenthesis so parser knows to parse it as a function expression instead of function declaration. What's the difference between a variable that is: null, undefined or undeclared? How would you go about checking for any of these states? - Correct Answers -In JavaScript, undefined means a variable has been declared but has not yet been assigned a value.

null is an assignment value. It can be assigned to a variable as a representation of no value. undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object. Undeclared variables are those that are not declared at all. The falsy values null and undefined are not equivalent to anything except themselves What is "lexical" scoping? - Correct Answers -JavaScript has lexical scoping with function scope. In other words, even though JavaScript looks like it should have block scope because it uses curly braces { }, a new scope is created only when you create a new function. To resolve variables, JavaScript starts at the innermost scope and searches outwards until it finds the variable it is looking for. Child has access to parent's variables, but not vice versa. What is a closure and how/why would you use it? - Correct Answers -A function within a function, a closure is an inner function that has access to the outer function's variables. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function's variables, and it has access to the global variables. Properties

  • Closures have access to the outer function's variable even after the outer function returns.
  • Closures store references to the outer function's variables Uses:
  • call a function that generates another function or group of functions but hides all the state in private variables within the closure
  • closures for events and callbacks
  • enforce public/private methods http://javascriptissexy.com/understand-javascript-closures-with-ease/ Explain AJAX in as much detail as possible - Correct Answers -Make http requests (use of the XMLHttpRequest object to communicate with server-side scripts). It can send as well as receive information in a variety of formats, including JSON, XML, HTML, and even text files.

function declaration - Correct Answers -The function declaration defines a function with the specified parameters. function name([param,[, param,[..., param]]]) { [statements] } https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions What are promises? What are the pros and cons of using Promises instead of callbacks? - Correct Answers -At their most basic, promises are a bit like event listeners except: -A promise can only succeed or fail once. It cannot succeed or fail twice, neither can it switch from success to failure or vice versa -If a promise has succeeded or failed and you later add a success/failure callback, the correct callback will be called, even though the event took place earlier ways of creating an object in JS - Correct Answers --Using object initializers (aka literal notation) var obj = { property_1: value_1, 2: value_2, "property n": value_n }; // or a string

  • Using a constructor function function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } var mycar = new Car("Eagle", "Talon TSi", 1993);
  • Using the Object.create method var Animal = { type: "Invertebrates", // Default value of properties displayType : function(){ // Method which will display type of Animal console.log(this.type); } } // Create new animal type called animal var animal1 = Object.create(Animal); animal1.displayType(); // Output:Invertebrates

http://stackoverflow.com/questions/6843951/which-way-is-best-for-creating-an-object-in- javascript-is-var-necessary-befor How do you go about testing your Javascript? - Correct Answers -Jasmine? How do you organize your code? - Correct Answers -Module pattern: meyer, config, base, layout, theme AMD vs. CommonJS? - Correct Answers -What? AMD Asynchronous Module Definition

  • less tooling involved
  • easier to debug
  • less edge cases that break in browsers src: http://requirejs.org/docs/whyamd.html What's the difference between .call and .apply? - Correct Answers -Apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly. function.apply(valueForThis, [arg1, arg2, ...]) function.call(valueForThis, arg1, arg2, ...) http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and- apply A useful mnemonic is "A for array and C for comma." Difference between document load event and document ready event? - Correct Answers -Document ready: executes when HTML-Document is loaded and DOM is ready Document load: executes when complete page is fully loaded, including all frames, objects and images Truthy and Falsy - Correct Answers -Falsy values: false 0 (zero) "" (empty string) null undefined NaN (a special Number value meaning Not-a-Number) The falsy values null and undefined are not equivalent to anything except themselves