sudheerj/javascript-interview-questions Flashcards
What are the possible ways to create objects in JavaScript
1. Object constructor: var object = new Object();
2. Object's create method: var object = Object.create(null);
3. Object literal syntax: var object = { name: "Sudheer" age: 34 };
4. Function constructor: function Person(name){ this.name=name; this.age=21; } var object = new Person("Sudheer");
5. ES6 Class syntax: class Person { constructor(name) { this.name = name; } }
var object = new Person(“Sudheer”);
What is a prototype chain
When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.
What is the difference between Call, Apply and Bind
They all attach this into function (or object) and the difference is in the function invocation (see below).
Call invokes the function and allows you to pass in arguments one by one.
Apply invokes the function and allows you to pass in arguments as an array.
Bind returns a new function, allowing you to pass in a this array and any number of arguments.
What is JSON and its common operations
JSON is a text-based data format following JavaScript object syntax, which was popularized by Douglas Crockford. It is useful when you want to transmit data across a network and it is basically just a text file with an extension of .json, and a MIME type of application/json
It has Parsing: Converting a string to a native object
JSON.parse(text)
Stringification: converting a native object to a string so it can be transmitted across the network
JSON.stringify(object)
What is the purpose of the array slice method
The slice() method returns the selected elements in an array as a new array object. It selects the elements starting at the given start argument, and ends at the given optional end argument without including the last element. If you omit the second argument then it selects till the end.
Note: Slice method won’t mutate the original array but it returns the subset as a new array.
What is the purpose of the array splice method
The splice() method is used either adds/removes items to/from an array, and then returns the removed item. The first argument specifies the array position for insertion or deletion whereas the optional second argument indicates the number of elements to be deleted. Each additional argument is added to the array.
Note: Splice method modifies the original array and returns the deleted array.
What is the difference between slice and splice
Slice:
- Doesn’t modify the original array(immutable)
- Returns the subset of original array
- Used to pick the elements from array
Splice:
- Modifies the original array(mutable)
- Returns the deleted elements as array
- Used to insert or delete elements to/from array
How do you compare Object and Map
Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. Due to this reason, Objects have been used as Maps historically. But there are important differences that make using a Map preferable in certain cases.
- The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.
- The keys in Map are ordered while keys added to Object are not. Thus, when iterating over it, a Map object returns keys in order of insertion.
- You can get the size of a Map easily with the size property, while the number of properties in an Object must be determined manually.
- A Map is an iterable and can thus be directly iterated, whereas iterating over an Object requires obtaining its keys in some fashion and iterating over them.
- An Object has a prototype, so there are default keys in the map that could collide with your keys if you’re not careful. As of ES5 this can be bypassed by using map = Object.create(null), but this is seldom done.
- A Map may perform better in scenarios involving frequent addition and removal of key pairs.
What is the difference between == and === operators
JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison. The strict operators take type of variable in consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,
What are lambda or arrow functions
An arrow function is a shorter syntax for a function expression and does not have its own this, arguments, super, or new.target. These functions are best suited for non-method functions, and they cannot be used as constructors.
What is a first class function
In Javascript, functions are first class objects. First-class functions means when functions in that language are treated like any other variable.
For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable. For example, in the below example, handler functions assigned to a listener
What is a first order function
First-order function is a function that doesn’t accept another function as an argument and doesn’t return a function as its return value.
What is a higher order function
Higher-order function is a function that accepts another function as an argument or returns a function as a return value or both.
What is a unary function
Unary function (i.e. monadic) is a function that accepts exactly one argument. It stands for a single argument accepted by a function.
const unaryFunction = a => console.log (a + 10);`
What is the currying function
Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with only a single argument. Currying is named after a mathematician Haskell Curry. By applying currying, a n-ary function turns it into a unary function.
Curried functions are great to improve code reusability and functional composition.