Search This Blog

Q46-Q50

Q46. How to check whether an object is iterable or not?
Q47. What is Symbol in JavaScript?
Q48. What is use of new loop for..of?
Q49. What are anonymous function in JavaScript?
Q50. What is the type of array in JavaScript?
-------------------------------------------------------------------------------------------------------------------------
Q46. How to check whether an object is iterable or not?

Answer 46:

As per ECMA6 all iterable objects must have a key Symbol.Iterator as part of their prototype.

eg
let myArray = ['2', '4', 45];
console.dir(myArray); // you will find Symbol.Iterator in console as prototpye property.

While for below code you wont find this key.

let myObject = {
key1: 'value1',
key2:'value2',
key3: ['a','b','c'],
key4: function() {return key1.value}
}
https://www.youtube.com/watch?v=ZNrJPzjNt-o
-------------------------------------------------------------------------------------------------------------------------
Q47. What is Symbol in JavaScript?

Answer 47:

Its a new primitive datatype added in ES6. It is use to generate a unique id but we never have access to value of that id.
In example below symbol will give a unique value to user_id making it perfect to act as an id but We will never get what value it get assigned.

let user_id = Symbol();
let user = {};
user[user_id] = 24;
console.log(user[user_id]);
console.log (typeof user_id); // Symbol

** you can not create it with new keyword. ie var user_id1 = new Symbol(); will throw error.
** anything in bracket of symbol is a tag name for that symbol. Symbol still outputs a unique id which value is not accessible to us.

https://www.youtube.com/watch?v=gSKtdtSCYfY
https://www.youtube.com/watch?v=Psdf5Bo1SFM (better)

-------------------------------------------------------------------------------------------------------------------------
Q48. What is use of new loop for..of?

Answer 48:

Its a new loop for  iterative objects like Array, String, TypedArray, Map, Set etc
for..of loops through properties' values of object.


https://www.youtube.com/watch?v=ZNrJPzjNt-o
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
-------------------------------------------------------------------------------------------------------------------------
Q49. What are anonymous function in JavaScript?

Answer 49: 

Defining a function without name is called anonymous function. 
eg 

var func1 = function () { 
//code
 }
-------------------------------------------------------------------------------------------------------------------------
Q50. What is the type of array in JavaScript?

Answer 50: 

In JavaScript arrays are of type object.
-------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment