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.