Search This Blog

Q36-Q40

Q36. What is TypedArray in JavaScript?
Q37. What is eval() in JavaScript?
Q38. What is yield* in JavaScript?
Q39. What new method for..in is different from for..of?
Q40. What are Sets and Weak Sets in JavaScript?
--------------------------------------------------------------------------------------------------------------------------
Q36. What is TypedArray in JavaScript?

Answer 36:

JavaScript typed arrays are array-like objects and provide a mechanism for accessing raw binary data. As you may already know, Array objects grow and shrink dynamically and can have any JavaScript value. Raw data could be like audio, video, web sockets etc.

However, typed arrays are not to be confused with normal arrays, as calling Array.isArray() on a typed array returns false
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
------------------------------------------------------------------------------------------------------------------------
Q37. What is eval() in JavaScript?

Answer 37:

The eval() function evaluates JavaScript code represented as a string.

var x = 2, y = 4;
console.log(eval('x + y'));  // Direct call, uses local scope, result is 6
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
----------------------------------------------------------------------------------------------------------------------
Q38. What is yield* in JavaScript?

Answer 38:

The yield* expression is used to delegate to another generator or iterable object.

(function(){
  console.clear();
  function* f1(){
   yield "f1";
  }

  function* f2(){
    yield* f1();
  }
  const iterator = f2();
  console.log(iterator.next());
  console.log(iterator.next());

})();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield*
------------------------------------------------------------------------------------------------------------------
Q39. What new method for..in is different from for..of?

Answer 39:
  • for..of is a new loop in JavaScript.
  • for..in loops through properties name of object.while for..of loops through properties values of object.

(function(){
  console.clear();
let fruits = ['apple','banana','carrot'];

//for-in loops through properties name of objects.
  for (let a in fruits)
    {
      console.log(fruits[a]);
    }

//for..of loops through properties values of object.
  for (let b of fruits)
    {
      console.log(b);
    }
})();

o/p
"apple"
"banana"
"carrot"
"apple"
"banana"
"carrot".
-----------------------------------------------------------------------------------------------------------------------
Q40. What are Sets and Weak Sets in JavaScript?

Answer 40:
  • The Set object lets you store unique values of any type, whether primitive values or object references.
  • In contrast to Sets, WeakSets are collections of objects only and not of arbitrary values of any type. 
  • The WeakSet is weak: References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakSet, they can be garbage collected. That also means that there is no list of current objects stored in the collection. WeakSets are not enumerable.


const set1 = new Set();
set1.add(1);
set1.add('string data');
let obj = {f:'Him', l:'Goel'};
set1.add(obj);
console.log(set1.has(1));
// expected output: true
console.log(set1.has('string data'));
// expected output: true
console.log(set1.has(obj1));
// expected output: true

No comments:

Post a Comment