JavaScript Interview Prep: Functions, Closures, Currying

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ค. 2024
  • Prepare for JavaScript interview questions focusing on closures, functions, and currying. Throughout the course, you'll delve into various aspects of JavaScript, such as function declarations, expressions, scopes, and hoisting, as well as learning about advanced concepts like closures and lexical scope. You'll also gain a deeper understanding of currying and its practical applications in JavaScript.
    Course created by ‪@RoadsideCoder‬
    Useful Links mentioned in the video -
    🎥 var, let and const Video - • Javascript Interview Q...
    🎥 map, filter, and reduce Video - • Javascript Interview Q...
    🎥 FE Interview Experience Video - • Frontend Interview Exp...
    ⭐️ Contents ⭐️
    ⌨️ (0:00:00) Intro
    ⌨️ (0:01:48) Function Declaration
    ⌨️ (0:02:13) Function Expression
    ⌨️ (0:02:34) Anonymous Function
    ⌨️ (0:03:25) First Class Functions
    ⌨️ (0:04:54) What is IIFE?
    ⌨️ (0:06:05) IIFE - Interview Question
    ⌨️ (0:06:59) Closures
    ⌨️ (0:07:27) Function Scopes
    ⌨️ (0:09:10) Function Scope - Interview Question
    ⌨️ (0:10:18) Hoisting in Functions
    ⌨️ (0:13:40) Hoisting - Interview Question
    ⌨️ (0:15:46) Params vs Arguments
    ⌨️ (0:16:25) Spread vs Rest Operators
    ⌨️ (0:17:43) Interview Question on params, args, spread, rest
    ⌨️ (0:19:03) Callback Function
    ⌨️ (0:20:02) Callback Function - Interview Questions
    ⌨️ (0:20:58) Arrow Functions
    ⌨️ (0:21:59) Arrow function vs Normal Function
    ⌨️ (0:25:13) Closures
    ⌨️ (0:25:50) What is Lexical Scope?
    ⌨️ (0:27:39) Lexical Scope - Interview Question
    ⌨️ (0:28:53) What is Closure?
    ⌨️ (0:29:44) Example of Closures
    ⌨️ (0:30:57) Why Closure?
    ⌨️ (0:32:20) Closure Scope Chain
    ⌨️ (0:35:13) Ques 1 - What will it print?
    ⌨️ (0:37:10) Ques 2 - Write a function for this
    ⌨️ (0:39:29) Ques 3 - Time Optimisation with Closures
    ⌨️ (0:42:08) Ques 4 - setTimeout + block scope with Closures
    ⌨️ (0:47:08) Ques 5 - Create a private counter
    ⌨️ (0:49:49) Ques 6 - What is Module Pattern?
    ⌨️ (0:51:40) Ques 7 - Make this run only once
    ⌨️ (0:54:18) Ques 8 - Once Polyfill Implementation
    ⌨️ (0:58:11) Ques 9 - Memoise/Caching Implementation
    ⌨️ (1:03:46) Ques 10 - Closure vs Scope
    ⌨️ (1:04:25) Currying
    ⌨️ (1:04:55) What is Curring in Javascript?
    ⌨️ (1:05:23) Example of Currying
    ⌨️ (1:08:17) Ques 1 - Implement sum(2)(6)(1)
    ⌨️ (1:11:02) Ques 2 - Reusing Variable for logic
    ⌨️ (1:14:09) Ques 3 - Infinite Currying
    ⌨️ (1:18:27) Ques 4 - Currying vs Partial Application
    ⌨️ (1:20:37) Ques 5 - Manipulating DOM
    ⌨️ (1:23:01) Ques 6 - curry() implementation

ความคิดเห็น • 109

  • @pranavbhat29
    @pranavbhat29 ปีที่แล้ว +29

    1:25:42 - I almost spent 10 minutes trying to figure out why he is using func.length >= args.length, only to realise a few seconds later he fixed the bug. Typically viewers might be actively thinking as you speak and such bugs can cause confusions, so I would humbly request processing the video beforehand to avoid such confusions.

  • @WinchesterD
    @WinchesterD 4 หลายเดือนก่อน +1

    Great course, from basic to advanced questions.

  • @abhishekmohanty232
    @abhishekmohanty232 9 หลายเดือนก่อน +16

    Thanks a lot man, I had watched this video a day before my interview and some of the interview questions were exactly the same , and I was able to successfully clear the interview in a really good product based company 😊

    • @samfisher8583
      @samfisher8583 9 หลายเดือนก่อน +1

      Were you hired?

    • @abhishekmohanty232
      @abhishekmohanty232 9 หลายเดือนก่อน +8

      Yes😊

    • @bablugupta2119
      @bablugupta2119 7 หลายเดือนก่อน

      which company brother?@@abhishekmohanty232

  • @j2devstudio
    @j2devstudio 3 หลายเดือนก่อน +1

    Piyush I found this video when I had 24 hours to prepare for a technical interview to test javascript proficiency. I have to tell you how much I appreciate your interview prep video because it seems I had forgotten many things. Honestly, some topics were never made so clear for me - you're a great teacher. I'll follow up here once I finish the interview, but I definitely owe you a coffee ! thank you

  • @divinesovereign3103
    @divinesovereign3103 6 หลายเดือนก่อน +1

    such an amazing content thank you so much! - a guy from the Philippines.

  • @solaraproject3614
    @solaraproject3614 9 หลายเดือนก่อน +1

    Here is an alternate explanation for "setTimeout + blockscope":
    "a" contains a for loop that iterates three times. Inside the loop, a setTimeout function is called for each iteration, with a delay that increases with each iteration (0, 1000, 2000 milliseconds). However, the callback function inside the setTimeout refers to the variable i, which is declared using var. Since var does not have block scope, the final value of i after the loop completes is 3.

  • @mayursmahajan
    @mayursmahajan 10 หลายเดือนก่อน +8

    Bro literally saved my life today, I watched this video before the interview, It went excellent.

    • @Antonailzb
      @Antonailzb 6 หลายเดือนก่อน +3

      did you get the job?

    • @mayursmahajan
      @mayursmahajan 6 หลายเดือนก่อน +8

      @@Antonailzb Yes bro, I started 3 months ago here.

    • @Antonailzb
      @Antonailzb 6 หลายเดือนก่อน +3

      @@mayursmahajan congrats bro! hope its been good to you

  • @hanius5438
    @hanius5438 2 หลายเดือนก่อน

    This is such a good prep, but also entertaining somehow, first time watching stuff like this.

  • @kvelez
    @kvelez 6 หลายเดือนก่อน

    Excellent course, great video.

  • @iuhere
    @iuhere ปีที่แล้ว +7

    I do like the way you explained it, and tried to keep the code simple and minimalistic to avoid confusion. This especially helps when you are exploring a complex concept. Apart from once function I was almost able to understand all, also in one go with small breaks,so the video was interesting enough to keep me attentive including some minor drink and stretch breaks. This is indeed a great valued content worth every single second of my watchtime but hey, we don't count in seconds , right? :)
    TLDR : Great content. Good luck for future ones, and this one helped a lot.

  • @sudhirkalivarapu97
    @sudhirkalivarapu97 ปีที่แล้ว

    Can a value can be shared between sibling functions in a function in closures

  • @yabuking84
    @yabuking84 8 หลายเดือนก่อน

    57:57 i though using apply() on arrow functions doesnt affect it?

  • @zzh315
    @zzh315 ปีที่แล้ว

    the manipulating DOM example does not seem to be of using currying, but more of using closure to store value of id.

  • @user-vb6gd7ug1w
    @user-vb6gd7ug1w ปีที่แล้ว +4

    תודה!

  • @lavish532
    @lavish532 ปีที่แล้ว +1

    absolutely best timing, I have an interview tomorrow. thanks FCC !

  • @lucien5112
    @lucien5112 ปีที่แล้ว +1

    Another solution for ques 6 at 1:23:01 -> "const curry = f => a => b => c => f(a, b, c)"
    expanded code for clarity
    function curry (f) {
    function layer1(a){
    function layer2(b){
    function layer3(c) {
    return f(a, b, c)
    }
    return layer3
    }
    return layer2
    }
    return layer1
    }

  • @ichiroutakashima4503
    @ichiroutakashima4503 ปีที่แล้ว

    Was I simply too afraid? Should I start applying? Are these really expected interview questions for entry level?

  • @ManishKumar-ud6kj
    @ManishKumar-ud6kj ปีที่แล้ว

    🎉 nice

  • @genacator
    @genacator ปีที่แล้ว

    Love the part on closures.

  • @aguarosada5
    @aguarosada5 ปีที่แล้ว

    thanks for this content will help me !

  • @natali_li90
    @natali_li90 ปีที่แล้ว +2

    Thank you so much for this video! Waiting for "this" video 😉

    • @RoadsideCoder
      @RoadsideCoder ปีที่แล้ว +1

      Thanks, You can check that video here - th-cam.com/video/rv7Q11KWmKU/w-d-xo.html

    • @Mohammedrabeeh-fg1ww
      @Mohammedrabeeh-fg1ww ปีที่แล้ว

      @@RoadsideCoder 👍👍

  • @elias-soykat
    @elias-soykat ปีที่แล้ว

    41:01 I still not using closure but i get the same performance boost :)
    function find(index) {
    let a = [];
    for (let i = 0; i < 10000000; i++) {
    a[i] = i * i;
    }
    console.log(a[index]);
    return function () {};
    }
    const closure = find(12);
    console.time("12");
    closure();
    console.timeEnd("12");

  • @teslavlogs8568
    @teslavlogs8568 ปีที่แล้ว

    Awesome 👏

  • @lihonghan324
    @lihonghan324 ปีที่แล้ว +4

    41:01 Both approaches should give almost constant value (the first approach is longer and the closure one is short). There is no async involved in the process, and we will run the large for loop either way no matter what is the input value. The reason closure is faster is not because there is any magic in it, but just you run the closure() before the counting and it is like a cache, storing the calculated array in the function scope. Let me know if my understanding is correct, otherwise, it does not make sense to me

    • @ujjwaldixit9777
      @ujjwaldixit9777 ปีที่แล้ว

      You are correct. The optimization comes into play when you need to call the anonymous() function multiple times with different indices. Without memoization, each call to find() would recompute the entire array on every invocation, resulting in significant overhead. By using memoization, you ensure that the array is computed only once and subsequent calls utilize the precomputed array, reducing redundant computations and improving performance.

  • @user-hw4td5zc1g
    @user-hw4td5zc1g ปีที่แล้ว

    i like the methods using shorthand syntax
    its like making your naming of functions more reusable

  • @monotroupe6294
    @monotroupe6294 ปีที่แล้ว

    Java interview prep next😊

  • @suplays
    @suplays ปีที่แล้ว +1

    Im already subscribed to his channel and love his content. ❤

  • @Imran_kpk
    @Imran_kpk ปีที่แล้ว

    Great❤

  • @sumeyyea
    @sumeyyea ปีที่แล้ว

    that's what i was waiting for 🏆 plss release more videos related to interview

  • @madhousenetwork
    @madhousenetwork ปีที่แล้ว

    Came in good time

  • @rashim
    @rashim หลายเดือนก่อน

    17:30 Its Rest *Parameter* and Spread *Operator*

  • @GAGANDEEPSINGH-fv2rt
    @GAGANDEEPSINGH-fv2rt ปีที่แล้ว

    This questions is very good for entry level js coding interview

  • @euroclydon7883
    @euroclydon7883 ปีที่แล้ว +6

    Roadside coder is here🔥🔥

    • @AbhituklVerse
      @AbhituklVerse ปีที่แล้ว

      Explain?

    • @sky_9460
      @sky_9460 4 หลายเดือนก่อน

      his channel name @@AbhituklVerse

  • @annalan1895
    @annalan1895 ปีที่แล้ว

    Could I cooperate with you on promotional video about software? Thanks

  • @cendolbang99
    @cendolbang99 ปีที่แล้ว

    this closure thingy is huge stuff I just discovered today, thank you!

  • @narinkotsuwan
    @narinkotsuwan ปีที่แล้ว

    สวัสดีค่ะขอบคุณค่ะ❤😂🎉😢😮😅😊

  • @alexanderkomanov4151
    @alexanderkomanov4151 ปีที่แล้ว

    Thanks!

  • @mrdarkdayy9902
    @mrdarkdayy9902 ปีที่แล้ว +3

    You know the tutorial is going to be superior than others when the guy is Indian.

  • @worldbest3097
    @worldbest3097 ปีที่แล้ว

    awesome!1

  • @ivailonetzov6662
    @ivailonetzov6662 ปีที่แล้ว +1

    Do a java job prep

  • @joshc2368
    @joshc2368 ปีที่แล้ว +1

    Definitely needed this

  • @songs123d
    @songs123d 4 หลายเดือนก่อน

    Treasure content.

  • @pranavbhat29
    @pranavbhat29 ปีที่แล้ว +2

    41:01 - Should it not take the same time in the non optimised implementation because the loop would run the same number of times irrespective of the index passed?

    • @lihonghan324
      @lihonghan324 ปีที่แล้ว

      have the same question, no idea why the time is different

    • @kuubeu
      @kuubeu ปีที่แล้ว

      the loop runs when the function is assigned (`const closure = find()`) - the variable `a` is then stored inside the newly created local scope and calling `closure(n)` only needs to read the value from the precomputed array

    • @pranavbhat29
      @pranavbhat29 ปีที่แล้ว

      @@kuubeu You did not understand the question which I asked, never mind. The non-optimized was not optimal because there was no precomputation, and hence in every invocation the array was created again and again, and that would be time-consuming. And optimizing in this context meant pre-computing so that the time to construct the array, again and again, is saved.
      However, if you do notice the code carefully in both versions, the size of the constructed array is the same ( 1000000 or 1 million ) and hence the time taken to construct the array should be the same theoretically be it in an optimal and sub-optimal version.

    • @kuubeu
      @kuubeu ปีที่แล้ว

      @@pranavbhat29 it does take the same amount of time if you run it only once, after that it's basically instant:
      unoptimized:
      find(a) // takes long
      find(b) // also takes long
      // ... all slow
      optimized:
      const closure = find() // takes long
      closure(a) // very fast
      closure(b) // very fast
      // ... all fast

    • @pranavbhat29
      @pranavbhat29 ปีที่แล้ว +1

      @@kuubeu
      In the video
      unoptimized:
      find(a) // takes t1 milliseconds
      find(b) // takes t2 milliseconds
      // and t1

  • @sushantkunkekar2155
    @sushantkunkekar2155 ปีที่แล้ว

    Life saviour

  • @k4nful
    @k4nful 9 หลายเดือนก่อน

    35:24 is so confusing because on hoisting 14:26 with var the behavior is not the same...

  • @NoHorizon-
    @NoHorizon- ปีที่แล้ว

    How did he move square() to console log? I mean what is she shortcut?

    • @ayaanshaikh6374
      @ayaanshaikh6374 ปีที่แล้ว

      That's called a cut in the video, my guy

    • @hrvojematosevic8769
      @hrvojematosevic8769 9 หลายเดือนก่อน +1

      You can cut the code by using keyboard shortcut ctrl + x, ctrl + v or use VS code shortcut by selecting text, holding alt key then moving it up and down with arrow keys.

  • @Khadi-C
    @Khadi-C ปีที่แล้ว

    Thanks for this! I'm saving this for when I'm ready for interviews.

  • @nick-pu4zae
    @nick-pu4zae ปีที่แล้ว

  • @user-hw4td5zc1g
    @user-hw4td5zc1g ปีที่แล้ว

    the infinite currying is like recursion

  • @yashvanthkumar2576
    @yashvanthkumar2576 7 หลายเดือนก่อน

    Some one explain how this is working
    function once(func, context) {
    let ran;
    return function() {
    if(func) {
    ran = func.apply(context || this, arguments)
    console.log("ssss", ran)
    func = null
    }
    return ran
    }
    }
    what does ran contains after running and how the function reinitialized to null?

    • @manikmahajan2603
      @manikmahajan2603 6 หลายเดือนก่อน

      - Depends on the return value of the func. As per the example in the video, func is console.log and returns undefined.
      So, ran will have a value of undefined.
      When the inner anonymous function is called for the first time, func is assigned a value of null.
      Then after that when anonymous function is called again, func will value null.
      So, it will not enter the if block.

  • @Argylleagen
    @Argylleagen 3 หลายเดือนก่อน

    20:00 🔖

  • @aimeneharbi3913
    @aimeneharbi3913 ปีที่แล้ว

    We need dart language

  • @billycris2878
    @billycris2878 ปีที่แล้ว +2

    I understood nothing

  • @criminalsnail757
    @criminalsnail757 ปีที่แล้ว +1

    Can someone explain 9:10?

    • @RoadsideCoder
      @RoadsideCoder ปีที่แล้ว +1

      To understand that better, you need to understand scope of var, let and const. Just watch my var,let,const video!

  • @official_ashhh
    @official_ashhh ปีที่แล้ว

    Referenceerror: function not working

  • @impulseimp1035
    @impulseimp1035 ปีที่แล้ว

    Dond dell me whad uoo duooo please 😅

  • @JOJO_THE_PROGRAMMER
    @JOJO_THE_PROGRAMMER 3 หลายเดือนก่อน

    47:45

  • @govindbisen388
    @govindbisen388 9 หลายเดือนก่อน

    Pdf de do

  • @forheuristiclifeksh7836
    @forheuristiclifeksh7836 18 วันที่ผ่านมา

    16:24

  • @princess_maya_
    @princess_maya_ ปีที่แล้ว +2

    There's a joke somewhere to be made here about currying lol

  • @forheuristiclifeksh7836
    @forheuristiclifeksh7836 18 วันที่ผ่านมา

    1:23:00

  • @jomareperocho
    @jomareperocho ปีที่แล้ว +2

    CURRYing

  • @ayushraj-fq4gn
    @ayushraj-fq4gn ปีที่แล้ว +9

    you were always referring to another video for another concept this video could have been good if all concepts were clear at the same time

  • @wb4529
    @wb4529 ปีที่แล้ว

    hehe

  • @m_yoda
    @m_yoda 8 หลายเดือนก่อน

    too much ads, every 3 minutes he advertised his product.

    • @RoadsideCoder
      @RoadsideCoder 7 หลายเดือนก่อน

      which product?

  • @rba1aji216
    @rba1aji216 ปีที่แล้ว

    bruh

  • @aammssaamm
    @aammssaamm ปีที่แล้ว +3

    Why do you need to yell? Do you think we are deaf?

    • @watchtube9429
      @watchtube9429 ปีที่แล้ว +1

      hahahahahahahahaahahahahahahahaahahahahahahahahaahahahah

  • @blogactivities5462
    @blogactivities5462 3 หลายเดือนก่อน

    this video leads new programmers to terrible mistakes / I do not recommend

    • @Argylleagen
      @Argylleagen 3 หลายเดือนก่อน

      Could you elaborate on that?

  • @josephlee4001
    @josephlee4001 ปีที่แล้ว +5

    The stock market's dividends motivated me to start investing. What counts, in my opinion, is that you will be able to live off of dividends without selling if you invest and make more money in addition to payouts. It suggests that you can give your children that advantage, giving them a head start in life. I've invested more than $600k throughout the years in dividend stocks; I'm still buying more today and will keep doing so until the price drops even further.

    • @johnrobert4538
      @johnrobert4538 ปีที่แล้ว

      It's always inspiring to hear from a veteran investor who has weathered the storm and come out on top. When your portfolio turns from green to red, it might be unsettling, but if you have invested in great companies, you should just keep adding to them and stick with your plan.

    • @bendickson9414
      @bendickson9414 ปีที่แล้ว

      I wholeheartedly concur, which is why I appreciate giving an investment coach the power of decision-making. Given their specialized expertise and education, as well as the fact that each and every one of their skills is centered on harnessing risk for its asymmetrical potential and controlling it as a buffer against certain unfavorable developments, it is practically impossible for them to underperform. I have made over 1.5 million dollars working with an investment coach for more than two years.

    • @bendickson9414
      @bendickson9414 ปีที่แล้ว

      There are many financial coaches who excel in their profession, but for the time being, I employ "Jackson Sten Marsh," because I adore his methods. You can make research and find out more.

  • @tejasukalkar2199
    @tejasukalkar2199 ปีที่แล้ว +22

    I've interview tomorrow and this video came as blessing in disguise for me thank you @freecodecamp😊😊❤❤

    • @dhiyanabdurazack5257
      @dhiyanabdurazack5257 ปีที่แล้ว +13

      I will pray for you to get the job when I sleep and when I am in the bus 🚌.
      Good Luck.
      i am going to travel in a 6 hours trip by bus with my mother to meet my sister and brother because of eid.

    • @About-world1777
      @About-world1777 ปีที่แล้ว +1

      Did you pass your interview

    • @629_nishantghadigaonkar3
      @629_nishantghadigaonkar3 11 หลายเดือนก่อน

      Can you tell me about your interview experience?

    • @abcproduction6819
      @abcproduction6819 9 หลายเดือนก่อน +2

      He failed miserably … nobody learn coding by watch TH-cam videos

    • @tejasukalkar2199
      @tejasukalkar2199 9 หลายเดือนก่อน

      @@abcproduction6819 No I didn't

  • @arkansavalder
    @arkansavalder หลายเดือนก่อน

    Internal Pointerrrr variable

  • @narinkotsuwan
    @narinkotsuwan ปีที่แล้ว

    สวัสดีค่ะขอบคุณค่ะ❤😂🎉😢😮😅😊

  • @bishalmahanta1765
    @bishalmahanta1765 ปีที่แล้ว

    25:19