Search This Blog

Q41-Q45

Q41. What are Maps and Weak Maps in JavaScript?
Q42. What are the differences between objects and maps?
Q43. Can we iterate maps with for...of loop?
Q44. What are the primitives data types in JavaScript?
Q45. Can we execute anonymous function multiple times in JavaScript?
------------------------------------------------------------------------------------------------------------------------
Q41. What are Maps and Weak Maps in JavaScript?

Answer 41:

The Map object holds key-value pairs. Any value (both objects and primitive values) may be used as either a key or a value.

like map, weakmap also holds key value pairs but Keys of WeakMaps are of the type Object only. Primitive data types as keys are not allowed (e.g. a Symbol can't be a WeakMap key).

var myMap = new Map();

  var keyString = 'a string',
    keyObj = {},
    keyFunc = function() {};

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, 'value associated with keyObj');
myMap.set(keyFunc, 'value associated with keyFunc');

  // getting the values
myMap.get(keyString);    // "value associated with 'a string'"
myMap.get(keyObj);       // "value associated with keyObj"
myMap.get(keyFunc);      // "value associated with keyFunc"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
------------------------------------------------------------------------------------------------------------------------
Q42. What are the differences between objects and maps?

Answer 42:

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

>> The keys of an Object are Strings and Symbols, whereas they can be any value for a Map, including functions, objects, and any primitive.
>> 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 may perform better in scenarios involving frequent addition and removal of key pairs.
>> 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.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
------------------------------------------------------------------------------------------------------------------------
Q43. Can we iterate maps with for...of loop?

Answer 43:

Yes

var myMap = new Map();
myMap.set(0, 'zero');
myMap.set(1, 'one');
for (var [key, value] of myMap) {
  console.log(key + ' = ' + value);
}
// 0 = zero
// 1 = one
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
------------------------------------------------------------------------------------------------------------------------
Q44. What are the primitives data types in JavaScript?

Answer 44:

String, Symbol, Null, Boolean, undefined, Number.
Remember is by SSN-BUN
------------------------------------------------------------------------------------------------------------------------
Q45. Can we execute anonymous function multiple times in JavaScript

Answer 45:

yes. As many times you want.
------------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment