Search This Blog

Q31-Q35

Q31. What are the two property of yield in JavaScript? How we migrate to next value
Q32. How to traverse elements on HTML using JavaScript? or How to traverse DOM elements using javascript?
Q33. What are local variables and global variables in JavaScript?
Q34. What are generators in JavaScript?
Q35. What is yield in JavaScript?
---------------------------------------------------------------------------------------------------------------------
Q31. What are the two property of yield in JavaScript? How we migrate to next value

Answer 31:

"done" and "value" are the two properties present in yield. 
Initially "done" remain false till iterator object reaches the end of object using .next() where "done" become true and "value" become "undefined".

with the help of .next() we migrate to next value.
---------------------------------------------------------------------------------------------------------------------
Q32. How to traverse elements on HTML using JavaScript? or How to traverse DOM elements using javascript?

Answer 32:

There are multiple ways we can traverse DOM elements using javascript  eg. document.querySelector is the most common way. 

You can traverse in three directions:
  1. Downwards
  2. Sideways
  3. Upwards
https://zellwk.com/blog/dom-traversals/
---------------------------------------------------------------------------------------------------------------------
Q33. What are local variables and global variables in JavaScript?

Answer 33:
JavaScript has three scopes – global, functional and block scope. Any variable declared outside of a function belongs to the global scope, and is therefore accessible from anywhere in your code. Each function has its own scope, and any variable declared within that function is only accessible from that function and any nested functions
---------------------------------------------------------------------------------------------------------------------
Q34. What are generators in JavaScript?

Answer 34:

With ES6 generators, we have a different kind of function, which may be paused in the middle, one or many times, and resumed later, allowing other code to run during these paused periods.

you use the new yield keyword to pause the function from inside itself. Nothing can pause a generator from the outside; it pauses itself when it comes across a yield.

However, once a generator has yield-paused itself, it cannot resume on its own. An external control must be used to restart the generator. 

Even more importantly, this stopping and starting is not just a control on the execution of the generator function, but it also enables 2-way message passing into and out of the generator, as it progresses. With normal functions, you get parameters at the beginning and a return value at the end. With generator functions, you send messages out with each yield, and you send messages back in with each restart.

Syntax:
function *foo() { // ..}

The way we control generator functions from the outside is to construct and interact with a generator iterator. We use is using the 'next' keyword


Good learning in below url after the heading "Generator Iterator"

https://davidwalsh.name/es6-generators
----------------------------------------------------------------------------------------------------------------------
Q35. What is yield in JavaScript?

Answer 35:

It can be thought of as a generator-based version of the return keyword. The yield keyword actually returns an IteratorResult object with two properties, value and done. The value property is the result of evaluating the yield expression, and done is false, indicating that the generator function has not fully completed.

(function(){
  console.clear();
  function* foo(index){
    while(index <2){
      yield index++;
    }
  }
  const iterator = foo(0);
  console.log(iterator.next());
  console.log(iterator.next());
  console.log(iterator.next());

})();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield

No comments:

Post a Comment