Questions III Flashcards
List down some of the features of ES6
Below are the list of some new features of ES6,
Support for constants or immutable variables
Block-scope support for variables, constants and functions
Arrow functions
Default parameters
Rest and Spread Parameters
Template Literals
Multi-line Strings
Destructuring Assignment
Enhanced Object Literals
Promises
Classes
Modules
What is ES6
ES6 is the sixth edition of the javascript language and it was released in June 2015. It was initially known as ECMAScript 6 (ES6) and later renamed to ECMAScript 2015. Almost all the modern browsers support ES6 but for the old browsers there are many transpilers, like Babel.js etc.
Can I redeclare let and const variables
No, you cannot redeclare let and const variables. If you do, it throws below error
Explanation: The variable declaration with var keyword refers to a function scope and the variable is treated as if it were declared at the top of the enclosing scope due to hoisting feature. So all the multiple declarations contributing to the same hoisted variable without any error. Let’s take an example of re-declaring variables in the same scope for both var and let/const variables.
Does the const variable make the value immutable
No, the const variable doesn’t make the value immutable. But it disallows subsequent assignments(i.e, You can declare with assignment but can’t assign another value later)
What are template literals
Template literals or template strings are string literals allowing embedded expressions. These are enclosed by the back-tick (`) character instead of double or single quotes. In ES6, this feature enables using dynamic expressions as below,
What is collation
Collation is used for sorting a set of strings and searching within a set of strings. It is parameterized by locale and aware of Unicode. Let’s take comparison and sorting features,
What is for…of statement
The for…of statement creates a loop iterating over iterable objects or elements such as built-in String, Array, Array-like objects (like arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.
What is the difference between internal and external javascript
Internal JavaScript: It is the source code within the script tag.
External JavaScript: The source code is stored in an external file(stored with .js extension) and referred with in the tag.
What is the purpose of Error object
The Error constructor creates an error object and the instances of error objects are thrown when runtime errors occur.
The Error object can also be used as a base object for user-defined exceptions.
What is the difference between a parameter and an argument
Parameter is the variable name of a function definition whereas an argument represents the value given to a function when it is invoked. Let’s explain this with a simple function
function myFunction(parameter1, parameter2, parameter3) {
console.log(arguments[0]); // “argument1”
console.log(arguments[1]); // “argument2”
console.log(arguments[2]); // “argument3”
}
What is the purpose of some method in arrays
The some() method is used to test whether at least one element in the array passes the test implemented by the provided function. The method returns a boolean value. Let’s take an example to test for any odd elements,
How do you combine two or more arrays
The concat() method is used to join two or more arrays by returning a new array containing all the elements.
The syntax would be as below,
array1.concat(array2, array3, …, arrayX)
What is the difference between Shallow and Deep copy
There are two ways to copy an object,
Shallow Copy: Shallow copy is a bitwise copy of an object. A new object is created that has an exact copy of the values in the original object.
If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied.
Deep copy: A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.
Does javascript uses mixins
Mixin is a generic object-oriented programming term
-is a class containing methods that can be used by other classes without a need to inherit from it.
In JavaScript we can only inherit from a single object. ie. There can be only one [[prototype]] for an object.
But sometimes we require to extend more than one, to overcome this we can use Mixin which helps to copy methods to the prototype of another class.
What is a thunk function
A thunk is just a function which delays the evaluation of the value.
It doesn’t take any arguments but gives the value whenever you invoke the thunk. i.e, It is used not to execute now but it will be sometime in the future.
What is the easiest way to convert an array to an object
You can convert an array to an object with the same data using spread(…) operator.
How do you create an array with some data
You can create an array with some data or an array with the same values using fill method.
What are wrapper objects
Primitive Values like string,number and boolean don’t have properties and methods but they are temporarily converted or coerced to an object(Wrapper object) when you try to perform actions on them.
For example, if you apply toUpperCase() method on a primitive string value, it does not throw an error but returns uppercase of the string.
What is AJAX
AJAX stands for Asynchronous JavaScript and XML and it is a group of related technologies(HTML, CSS, JavaScript, XMLHttpRequest API etc) used to display data asynchronously. i.e. We can send data to the server and get data from the server without reloading the web page.
Below are the list of different ways to deal with Asynchronous code.
Callbacks
Promises
Async/await
Third-party libraries such as async.js,bluebird etc
What are tasks in event loop
A task is any javascript code/program which is scheduled to be run by the standard mechanisms such as initially starting to run a program, run an event callback, or an interval or timeout being fired.
All these tasks are scheduled on a task queue. Below are the list of use cases to add tasks to the task queue,
When a new javascript program is executed directly from console or running by the
element, the task will be added to the task queue.
When an event fires, the event callback added to task queue
When a setTimeout or setInterval is reached, the corresponding callback added to task queue
What is microtask
Microtask is used for the javascript code which needs to be executed immediately after the currently executing task/microtask is completed. They are kind of blocking in nature. i.e, The main thread will be blocked until the microtask queue is empty. The main sources of microtasks are Promise.resolve, Promise.reject, MutationObservers, IntersectionObservers etc
Note: All of these microtasks are processed in the same turn of the event loop.
What is heap
Heap(Or memory heap) is the memory location where objects are stored when we define variables. i.e, This is the place where all the memory allocations and de-allocation take place. Both heap and call-stack are two containers of JS runtime. Whenever runtime comes across variables and function declarations in the code it stores them in the Heap.
What is an event table
Event Table is a data structure that stores and keeps track of all the events which will be executed asynchronously like after some time interval or after the resolution of some API requests. i.e.
Whenever you call a setTimeout function or invoke async operation, it is added to the Event Table. It doesn’t not execute functions on it’s own. The main purpose of the event table is to keep track of events and send them to the Event Queue as shown in the below diagram.