Javascript Interview Questions ( Closures ) - Lexical Scope, Output based Questions, Polyfills

Поделиться
HTML-код
  • Опубликовано: 26 дек 2024

Комментарии • 160

  • @RoadsideCoder
    @RoadsideCoder  2 года назад +2

    🔴 Get my Complete Frontend Interview Prep course - roadsidecoder.com/course-details
    ➡ Book an Interview Preparation call with me ( 20% OFF for limited time ) - topmate.io/roadsidecoder
    🟪 Follow me on Instagram and u will clear your interview 🤓 - instagram.com/roadsidecoder/

  • @ankitasingh734
    @ankitasingh734 Год назад +10

    These series are true GOLD, if possible keep on adding.

  • @hmehta051
    @hmehta051 2 года назад +39

    Great content, i recently gave a interview in cleartrip they asked this 4 question 1) write polyfill for promise 2)deep comparison of object 3) polyfill for call, apply,bind 4) polyfill for map, reduce,filter, forEach. It's help me and others if you can cover this questions

    • @ayushmukhopadhyay7470
      @ayushmukhopadhyay7470 2 года назад +5

      Did u apply for entry level job ?

    • @saikumargatla4706
      @saikumargatla4706 11 месяцев назад +1

      Did they asked DSA. Will it be ok if I will solve the DSA questions in c++ instead of javascript. What do you think

    • @hmehta051
      @hmehta051 11 месяцев назад +1

      They hiring for ui developer so doing DSA in javascript gives you advantages over other candidates

  • @ShivamSingh-ct1ix
    @ShivamSingh-ct1ix 2 года назад +4

    I never found something like this on youtube
    Gr8 job man

  • @DS-zr9gv
    @DS-zr9gv Год назад +6

    Through middle of this session and I already feel enlightened. Thank you so much :D

  • @JudoboyAlex
    @JudoboyAlex 2 года назад +9

    Please do asynchronous/ concurrency javascript interview questions. They are the toughest. Thanks for this!

  • @AmanSingh-qr3ld
    @AmanSingh-qr3ld 2 года назад +4

    Worthy 40 minutes.
    Keep sharing this kind of videos :-)

  • @vinaypatil8009
    @vinaypatil8009 2 года назад +2

    This series is god level series 🔥🔥🔥

  • @ankushladani496
    @ankushladani496 2 года назад +4

    Most awaited video....👏💥

  • @friendshipwithjavascript
    @friendshipwithjavascript 2 года назад +1

    UseFul Content With Endless Wishes

  • @abhishekvishwakarma9045
    @abhishekvishwakarma9045 2 года назад +4

    your Interview series is amazing 🔥 and worth watching 🚀

  • @iamankitdadhich
    @iamankitdadhich 7 месяцев назад

    use IIFE to make it work with var
    for (var id = 1; id

  • @rxtechandtrading
    @rxtechandtrading 2 года назад +1

    This guy is great - good content and personality to

  • @sunnykalita675
    @sunnykalita675 8 месяцев назад +3

    Bro thanks for sharing such knowledge in your videos but at the same time you need to make sure you are doing it right as so many people are watching these content , here in memoise polyfill your code is mistaken Json.stringyfy(...args) will not return us "500,500" in res object instead it will just jive us the first value of the array as we are not iterating the args array therefore even if we pass (500,500) and (500,600) as args we will get the same result and in the second function call res will mark the args as present in the object already, so to avoid we can do argsCache = args.join(",") then it will store in the res object as explained in the video
    by the way thanks for your content.
    happy coding to all

  • @itsMohak
    @itsMohak Год назад

    You are terrific.

  • @AK-uu3zj
    @AK-uu3zj 2 месяца назад

    What did I just watch! Going to purchase your course 🎉

  • @ankushladani496
    @ankushladani496 2 года назад +1

    Great.... content quality...

  • @saikumargatla4706
    @saikumargatla4706 11 месяцев назад

    Hey Piyush that Time Optimisation part was something new that I learnt today 16:20. Can you explain the the exact reason what closures did such that it took less time to execute the function

  • @anubhapant5227
    @anubhapant5227 9 месяцев назад

    Thankyou for this knowledge packed closures' video Pyiush. It is the most detailed video I found on RUclips and it covers all the important questons. Please make more of such content 🖖👍👌

    • @RoadsideCoder
      @RoadsideCoder  9 месяцев назад

      Thanks! I have created an indepth course covering all such topics - roadsidecoder.com/course-details

  • @AnandKumar-dc2bf
    @AnandKumar-dc2bf 2 года назад +3

    Videos are good. But one suggesting instead of asking us to refer ur previous video just explain the functionality in one line
    & it will be much much helpful...

  • @JohnDoe-q2g7n
    @JohnDoe-q2g7n 4 месяца назад

    "At 22:51, wouldn't creating the inner function inside the loop lead to a new function being created in memory every time? I think a better approach would be to define the function outside the loop, so it's created only once. This way, the function can be reused, which is more efficient."
    function print(num) {
    setTimeout(()=>{
    console.log(num)
    },num * 1000)
    }
    for(var i = 0;i

  • @emilyaung2963
    @emilyaung2963 2 года назад +4

    For those of you wondering how to fix the var scope issue with closure.
    const c = () => {
    for(var i =0; i < 3; i++){
    ((i) =>{
    setTimeout(() => {
    console.log(i);
    },1000);
    })(i);
    }
    }
    c();

    • @bhupendersoni5467
      @bhupendersoni5467 Год назад +1

      function a(){
      for(var i = 0; i {
      console.log(val)
      }, i*1000);
      }
      }

    • @praveengautam4689
      @praveengautam4689 Год назад

      for(var i = 0 ; i < 3 ;i++){
      (function(i){
      setTimeout(()=>{
      console.log(i)
      }, 1000*i);
      })(i)
      }

  • @salmanSyntax
    @salmanSyntax 2 года назад

    I genuinely enjoy your videos. will be giving interview in 2 months. Keep up the good work.

  • @juniorWeb247
    @juniorWeb247 2 года назад

    such a wonder full video with superb explanation,. Thanku so much for this information.

  • @JohnDoe-q2g7n
    @JohnDoe-q2g7n 4 месяца назад

    At 34:00 , The apply method takes the array of arguments but you are passing it separately just like we pass in the call method, how does it work?

  • @A1996ARP2075
    @A1996ARP2075 2 года назад +2

    Great content on closures keep making more and also make on React challenges which are asked in interviews

  • @arpithiside
    @arpithiside 2 года назад

    What a wonderfule video!! Got to learn so much.. thanks a lot ❤❤❤

  • @abhishekchaurasia_
    @abhishekchaurasia_ 10 месяцев назад

    Your explanation is very clear and the content is very useful. Can you make series for MERN interview questions

  • @ShubhamPatil-rr4on
    @ShubhamPatil-rr4on 2 года назад +1

    Great awesome amezing ....♥️💯
    I don't have words for you ...
    Really nice content.....
    Keep it up

  • @hellisayhello
    @hellisayhello 2 года назад +2

    Hi RoadsideCoder, great lessons, your explanations are very clear, thank you for all this work!
    I still have a question about "Question 8 - Once Polyfill Implementation"
    You pass a "context" argument and then apply it to the function, but I don't quite understand in which cases it would be useful?
    In your example "this, arguments" are applied, and everything would work even if we delete "context" from the code.
    When is it useful to pass a "context" and what kind of data is expected for this argument?

  • @Lavkushkumar-cn9me
    @Lavkushkumar-cn9me 2 года назад +1

    Waiting... Hope we also get some logical type questions in it

  • @jenso413
    @jenso413 2 года назад

    ahh this video finally made a bunch of things click for me, thanks man!

  • @elton-react-dev
    @elton-react-dev Год назад

    Amazing 🔥🔥🔥 thanks for that.

  • @TravelwithPujarini
    @TravelwithPujarini 2 года назад

    Excited for this video 🤩

  • @aditiagarwal7574
    @aditiagarwal7574 6 месяцев назад

    Amazzingg 🎉🔥

  • @subhashgn1775
    @subhashgn1775 2 года назад +1

    Thank you for the great resource.🤝
    very grateful to your contribution man🙌..

  • @A9kit.k
    @A9kit.k 2 года назад

    Thanks Piyush brother

  • @orz5516
    @orz5516 Год назад

    please add some algorithm questions and class / constructor functions question which are often ask in interviews.
    for example: implement a MinStack.
    By the way, a friend of mine got the setTimeout with var and let question in his last interview.

    • @RoadsideCoder
      @RoadsideCoder  Год назад +1

      here are the algorithm questions - ruclips.net/p/PLKhlp2qtUcSZtJefDThsXcsAbRBCSTgW4

  • @shortflicks83
    @shortflicks83 Год назад

    Superb dear

  • @prodbytej4312
    @prodbytej4312 27 дней назад

    Thank You Bro!!!

  • @lakshayoza
    @lakshayoza 2 года назад +2

    make a video on async await and callback hell

  • @ganeshtiwari2550
    @ganeshtiwari2550 2 года назад +1

    hello sir
    like i m from commerce to learn deeper n easy understanding before entrying in full stack should i do any course before that can u say plz

  • @kedarkulkarni0074
    @kedarkulkarni0074 2 года назад

    Eyes on fire🔥🔥

  • @rajsundarm48
    @rajsundarm48 Год назад

    Answer for 21:48 timeline
    var funcs = [];
    function createfunc(i) {
    return function() {
    console.log("My value: " + i);
    };
    }
    for (var i = 0; i < 3; i++) {
    funcs[i] = createfunc(i);
    }
    for (var j = 0; j < 3; j++) {
    funcs[j]();
    }
    Hope it helps for you guys

    • @praveengautam4689
      @praveengautam4689 Год назад

      for(var i = 0 ; i < 3 ;i++){
      (function(i){
      setTimeout(()=>{
      console.log(i)
      }, 1000*i);
      })(i)
      }
      this is what roadside coder wants

  • @RahulChauhan-lm8mk
    @RahulChauhan-lm8mk 2 года назад

    Many thanks for this video. It's really helpful to clear my concepts. My praises and lots of love to you.🙌❤

  • @yasharma2301
    @yasharma2301 2 года назад +1

    Great Video! Design patterns at the end of series?

  • @pranshuadhwaryu6374
    @pranshuadhwaryu6374 Год назад +2

    100 of years of Reasearch lol 0:12

  • @AjithKumarsham
    @AjithKumarsham Год назад +1

    Nice video but you did not clearly explain why the time got reduced in the time optimization :(
    I'm assuming it is meoizing the values underthe hood!

  • @RachithaJR
    @RachithaJR Год назад

    @Roadsidecoder For the time optimization without using closure part , as i give higher number time taken is less, any idea why is this happening?
    function find(index)
    {
    let a=[];
    for(let i=0;i

  • @aquibmohd
    @aquibmohd 2 года назад

    17:47 why did this take less time please explain?!

  • @cricketkeeda82
    @cricketkeeda82 2 года назад +1

    Great brother very helpful ❤️👏
    Bhai please ek personal portfolio website making ke bare may sikhao next video may please 🥺

    • @RoadsideCoder
      @RoadsideCoder  2 года назад

      Yes, video for that will be coming soon!

  • @ajaysankapal3175
    @ajaysankapal3175 2 года назад

    how 'ran' variable storing the value return by 'func.apply()', because 'apply()' method does not return any value?

  • @amishapandey-v1l
    @amishapandey-v1l 9 месяцев назад

    i didn't understand how closures helped in question 3 - Time Optimization .
    Can anyone please help?

  • @naveen9882
    @naveen9882 Год назад

    17:40 how did closure saved time can anyone explain ???

    • @ashwinbhargava3011
      @ashwinbhargava3011 Год назад +2

      In the previous `find` function the loop which filled the array runs a million times on each call. So when you called it twice, first for number 6 and then second for number 12, the loop ran twice for total of 2 million. Basically the loop was running on every call. The optimized `find` function ran the loop once and then returned a function which will just log out the array index value. So when the host run `const closure = find();`, the loop runs and an array is created and a function is returned which is stored in `closure` variable. Now when you use closure variable like `closure(6)` or `closure(12)`, the loop won't run as it has already run and the filled array is referenced to closure function via the concept of closures. So you may run the function 10 times the loop will run only a million times contrasting the previous function which would have ran 10 * million times. Feel free to ask a follow up.

    • @naveen9882
      @naveen9882 Год назад +1

      ​@@ashwinbhargava3011damn, it's clear now.
      Thanks for the explanation 🤜🤛

  • @anandstephan6020
    @anandstephan6020 2 года назад

    next video please on how this concept in applied in ReactJS

  • @pavithrasai1392
    @pavithrasai1392 2 года назад

    In the private counter example, is the _counter variable not initialized every time the add or retrieve methods are called?

  • @tmoneyy5697
    @tmoneyy5697 2 года назад +1

    I love your channel bro. Are you a self taught developer? I’m trying to learn react js and struggling a little bit

    • @RoadsideCoder
      @RoadsideCoder  2 года назад +1

      Yes I'm a self taught dev, contact me on Twitter and I'll help u out!

  • @aviii24x7
    @aviii24x7 10 месяцев назад

    what area all those extensions creates yellow line to tell bracket closings etc

  • @siriusplayz5871
    @siriusplayz5871 Год назад

    the example at 10:01 is function currying, right?
    @roadsideCoder

  • @abin_4857
    @abin_4857 2 года назад

    please make some videos on React interview questions.

  • @mayankpatel2062
    @mayankpatel2062 Год назад

    For revision :
    Q)What is closure?
    A)The function bundled with its lexical environment closed to that function
    simply function + Lexical environment

  • @manibansal8047
    @manibansal8047 2 года назад

    Hi Piyush,
    As per Ques no. 1 on line number 6 if (count ===0) so count is taking value from outer scope variable , but in below case it is throwing error : VM654:3 Uncaught ReferenceError: Cannot access 'x' before initialization
    at abc (:3:17)
    at :7:1
    let x = 9
    function abc(){
    console.log(x); // It should print 9 as in Ques no. 1 count was accessing value from outer scope which is global
    let x = 5;
    console.log(x);
    }
    abc();
    Please explain

    • @AjithKumarsham
      @AjithKumarsham Год назад

      Dood it's because of let and const is in temporal dead zone, If condition only executes it's respective code in it's block since let/const has block scope, In his example, count was not there in the function block it' was inside if block. but for your case since there's no block, It first check abc's scope while executing the code, so in the execution context it will first store x in temporal deadzone, so you have to initialize before accessing, Hope this answers your question

  • @kashmirtechtv2948
    @kashmirtechtv2948 5 месяцев назад

    Why do we need to call a function and save it im variable and then again call it with that variable????

  • @musiclovers8488
    @musiclovers8488 2 года назад

    what will happen if we create simultaneously two function inside of a function?? like :-
    function a()
    {
    function b()
    {
    return "this is clouser or not?";
    }
    function c()
    {
    return "is this a closure also ?";
    }
    }

    • @mattwayne9128
      @mattwayne9128 2 года назад

      Nothing. Because neither function ends up getting called.

  • @pavithrasai1392
    @pavithrasai1392 2 года назад

    In the once polyfill, can we use call instead of apply? If yes then how? Also you are modifying function param to null right, won't that cause any issues?

    • @RoadsideCoder
      @RoadsideCoder  2 года назад +1

      Yeah, in call you can spread the arguments array

  • @rakaa31
    @rakaa31 2 года назад +1

    i wanted to ask! from where you learnt JS??

  • @sagar7929
    @sagar7929 2 года назад

    Hello sir
    How are you?
    Please do more new series on nodejs with socket that use real world application!
    Thank you❤️

    • @RoadsideCoder
      @RoadsideCoder  2 года назад +1

      Yeah, Definitely bro ❤️

    • @sagar7929
      @sagar7929 2 года назад

      @@RoadsideCoder can't wait your new series and please do more series on nodejs expressjs🙏🏽🙏🏽
      Thank you❤️🙏🏽

    • @RoadsideCoder
      @RoadsideCoder  2 года назад +1

      Follow me on Instagram @RoadsideCoder to stay updated!

    • @sagar7929
      @sagar7929 2 года назад

      @@RoadsideCoder sure sir

  • @Raksha-s8t
    @Raksha-s8t 8 месяцев назад

    case 1 :
    function x(){
    var a = 7;
    function y(){
    console.log(a);
    var a = 5;
    }
    y();
    };
    x();
    output : undefined
    case 2 :
    function x(){
    var a = 7;
    function y(){
    console.log(a);

    }
    y();
    };
    x();
    output : 7
    case 3 :
    function x(){
    var a = 7;
    function y(){
    var a = 5;
    console.log(a);

    }
    y();
    };
    x();
    output : 5

  • @hamzashaikh9164
    @hamzashaikh9164 2 года назад

    Wooow🔥🔥🔥🔥

  • @annazacharz3959
    @annazacharz3959 2 года назад

    wouldn't the function
    function likeTheVideo(){
    if(view !=undefined){
    return;
    }
    view = 'dave'
    console.log("subscribe to ",view)
    } would also allow soolve problem 7 ? Awesome video btw

  • @kedarkulkarni0074
    @kedarkulkarni0074 2 года назад

    Make this run only once question.. can we do it using Boolean? By default var will be false once it's called it becomes true

  • @vikrambam1184
    @vikrambam1184 2 года назад

    Vikas
    Vishal

    i have to change all li :
    script :- let li = document.querySelectorAll('li')
    li.forEach(elm=>{
    for(let i = 0; i

    • @ankitmehrotra8519
      @ankitmehrotra8519 2 года назад +1

      li. Foreach((el, indx) => el. Textcontent = indx)

  • @kashmirtechtv2948
    @kashmirtechtv2948 5 месяцев назад

    But as a React js Developer, i have never used apply, bind, this

  • @anandoganiya9070
    @anandoganiya9070 2 года назад

    Can you make for react

  • @dheeraj2729
    @dheeraj2729 2 года назад

    Could you please do nodejs projects

  • @abhaaslakra2076
    @abhaaslakra2076 Год назад

    How many years of exp do you have? I'm preparing for 2 years and and I'm really very exhausted

  • @SalmanShah95
    @SalmanShah95 2 года назад

    I am following your MERN chat app playlist and I need your help .Can you help me?

    • @RoadsideCoder
      @RoadsideCoder  2 года назад

      Sure, contact me on instagram @RoadsideCoder

  • @gauravdhenge6328
    @gauravdhenge6328 Год назад

    for(var i=0;i

  • @manishbhardwaj4587
    @manishbhardwaj4587 2 месяца назад

    can someone explain me question number 3 please????

  • @muziahmed3419
    @muziahmed3419 Год назад

    In the question at 27.40 i.e question no 7, the functionality which you wanted is easily acheived by the below code,
    let view = 'Roadside Coder ';
    let num = 0;
    const singleFunction = ()=>{
    if(num > 0){
    console.log('already Subscribed')
    } else {
    console.log('subscribe to' + view)
    num++
    }


    }
    what is the difference between the code which you wrote and the above code? In my approach, I didn't had to create inner function.

    • @ViSiOn-mtnr
      @ViSiOn-mtnr Год назад

      You can do that as well as you have created a global counter but in video the counter is a local variable.
      Every time you call that function, the counter will be 0 this is why closure is used to store the reference of previous count

  • @InDiAnAjOnEs0002
    @InDiAnAjOnEs0002 Год назад

    I'm not getting the 100 years of good luck, what needs to be done?

  • @mohininarwade1108
    @mohininarwade1108 Год назад

    Nice video but you did not explain why the time got reduced in the time optimization question… because of this still I have no clarity of the use of closures… can you please reply??? also I saw that you don’t even replies to your subscribers comments… please start replying

  • @mohd.irfanlohar4544
    @mohd.irfanlohar4544 7 месяцев назад

    let count = 0;
    (function caller() {
    if(count === 0){
    var count = 1;
    console.log(count);
    }
    console.log(count);
    })(); can anybody explain why this code gives undefined only

  • @A9kit.k
    @A9kit.k 2 года назад

    Waiting

  • @tejusgroup6695
    @tejusgroup6695 2 года назад

    💯👌👌

  • @kewalkandpal
    @kewalkandpal 2 года назад

    🔥🔥🔥🔥

  • @neerajmali-dx5dq
    @neerajmali-dx5dq Год назад

    Sir last ke 2 samjh me nahi aayebilkul bhi context and aaplly

  • @jrandy
    @jrandy Год назад +1

    Wrong. A closure is not a function

  • @emilyaung2963
    @emilyaung2963 2 года назад

    did I just see hair extension ads on your browser ? lol

  • @vardansharma6020
    @vardansharma6020 Год назад

    -var doesnt have a block scope !!
    -private counter
    Module pattern private functions are not returned

  • @rahulmishraER
    @rahulmishraER 2 года назад

    Nice way of explaining Buddy ....
    Try explaining in Hindi too .... Might increase lexical scope 😀 Thanks for researching and sharing knowledge 👏

  • @friendshipwithjavascript
    @friendshipwithjavascript 2 года назад

    Love 💕 from Pakistan

  • @vijaykumar-ps5pq
    @vijaykumar-ps5pq 7 месяцев назад

  • @neerajmali-dx5dq
    @neerajmali-dx5dq Год назад

    Sir please try to explain in hindi

  • @TrainedFresher
    @TrainedFresher 8 месяцев назад

    function Namedis(){
    var name="aksuman";

    }
    Namedis()
    console.log(name)
    iam doing same thing but i am not getting error with this why?? unable to find answer plz help

    • @sachinmishra803
      @sachinmishra803 7 месяцев назад

      Please change your variable name because name is a reserved keyword in js

  • @Pareshbpatel
    @Pareshbpatel 2 года назад

    {2022-05-18}

  • @JunaidKhan-ln6pw
    @JunaidKhan-ln6pw 4 месяца назад

    function a() {
    for (var i = 0; i < 3; i++) {
    (function(i) {
    setTimeout(() => console.log(i), i * 1000);
    })(i);
    }
    }
    a();

  • @yaash.guptaa
    @yaash.guptaa 7 месяцев назад

    You literally do 100 of years of research?🤣🤣🤣🤣

  • @shatnuu_
    @shatnuu_ Год назад

    stop smoking weed so high..

  • @akankhyapanda7490
    @akankhyapanda7490 2 года назад

    function fetchUsers(url, callback) {

    setTimeout(
    () =>
    callback([
    { name: "John", id: 1 },
    { name: "Doe", id: 2 }
    ]),
    100
    );
    }
    function getUsers() {
    let users;

    fetchUsers(`/api/v1/users`, (res) => {
    console.log(res);
    users = Object.assign([], res);
    console.log(users);
    });
    return users;

    };
    function logUsers() {
    console.log("Users are: ", getUsers());
    }
    logUsers();How i can solve this for console.log