Top 10 JavaScript Interview Questions ( Part 2 )
- January 2, 2024
- Posted by: MainInstructor
- Category: C Go JavaScript
![*](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2024/01/1704190902_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: Top 10 JavaScript Interview Questions ( Part 2 )
Hello and welcome to TechSith Tutorials. This is a second part of top 10 JavaScript interview questions. The first part we covered the first 5 questions and this tutorial we are going to cover the rest of the 5 questions. So if you haven’t looked at the first tutorial, you can do it here
By clicking on the link here which I will provide So our sixth question is, “What is Prototypal Inheritance?” Eh.. it’s hard to pronounce actually. Or, you can call it Prototype based inheritance So clever ways to answer this is simpler umm answer. Basically, every object has a property called Prototype where
You can add methods and properties to it. And when you create other objects from this object the newly created object will automatically inherit the property of the parent. But not by including in its own property but instead it uses from it’s parent prototype properties and methods. And the way it works is
When you call a particular method on an object it first looks at it’s own properties to see if it’s there if it’s not there then it will look at it’s parent. And if it finds it there then it will execute it. So, this way the objects are much
Lighter and doesn’t carry all this methods with it. For example, if you carried a thousand objects, you won’t have to include all of those methods. It’s automatically available to you. So, that’s what Prototype inheritance means. You can also show this example where I am creating this object called car.
Which is basically, nothing but a constructor. Which has one argument, model and it’s basically setting this.model to this argument As you can see, car has this prototype property where I can attach a method called getModel which is actually outside. It is not a part of this constructor it’s outside.
And I can attach this method which simply returns the value of the model. So that creates let a toyota from it by using a car constructor and if I try to get the model information, it its automatically..I can call this get model on toyota Now, if I create let’s say a Nissan.
I just say Nissan If I run this I will get toyota and nissan So, this is what prototype inheritance is. Now the probability of getting this question in an interview is I would say about 30% A lot of time when you have an interviewer who wants to go deep
And want to test your object oriented skills they would ask this question. So the next question is “What is the difference between function declaration and function expression?” So, instead of answering this question you can just write it on the board saying function declaration as you can see is basically
The syntax for it differs, it has a function and a function name compared to the function expression which is an anonymous function that is saved into a variable. So, that is the main difference, there is a variable here and no variable there. The second difference is besides syntax
If I execute this function before it is declared here I am declaring it and I’m if I try to execute before this declaration I would get function declaration giving me this console.log which means it is available to me even before it’s definition. However, the function expression is not.
Because it’s saved into a variable it behaves like a variable And it has variable scope as well So, that is why it won’t be available to you before its leveraged. Another thing is when you want to pass a function to another function You cannot pass a function declaration to
Another function, you have to use a function expression because it’s a variable so you can pass a variable to another function. Now, the probability of getting this question is about 5% Now, the next question is “What is promises and why do we use it?”
Describing this answer is sometimes very difficult but, you can simplify it saying that when you wanna make an async call which has to wait for something to happen and then once it comes back you exit with a callback function And within that callback’s function you might do another
Ajax call which can wait for another result and you would have another callback function on success or failure and it can become kind of nested callback function hell Which is kind of ugly and it’s hard to read and debug and everything else So, that’s why to simplify this thing we use promises.
So, here is an example I have an ajax call I’m using jQuery ajax So basically when I call this, it waits for this to happen And when it returns a result it’s making another call and and ultimately it’s appending the result to an element
I can do the same thing using a promise and I would define a promise which has a callback function with a resolve and a reject I don’t have to do anything I just get the promise out Now, I can use this promise somewhere else. So the difference as you can see
This is more more like a nested function. Here, I have a promise in a separate line then… I will wait for a promise to complete and then this then function waits for a function to complete And when it returns, it would take the result and do something with it
And if I were to do another call I will just return the promise again and I would get it out and i can call the then function again on that promise And I can chain it like this compared to this which is quite ugly
So it doesn’t do anything special it’s nice to look at, it’s nice it’s easy to debug. And the probability of getting this question I would say probably 30% I also have a tutorial on promises if you want to check it out I have provided the link here
Alright, so the next question is on setTimeout() and you’re probably not going to get “What is a setTimeout?” Rather you might get a little puzzle where there will be something with a setTimeout And you have to determine what that outcome is. So, typically if I have a console log, right?
If I do ‘a’ um and if I do console.log that said ‘b’ so when I execute it I would see a, b, and c Now, what I will do here is sort of ‘a’ here, I would wrap it in a setTimeout() So, I would say something like this set
And inside I would have a function a callback function, I would just uh put the console.log inside of here And for the timer, I will just use zero seconds. And that is the trick they will use zero seconds so, now they will ask you “okay, what is the order of this?”
You might have assumed “Okay because if there is a zero time out there is no time out” Right? So, we say a, b, and c But the answer is it’s actually b, c and a Even though you have zero timeout that doesn’t mean it will exit right away
And that is because when you do a setTimeout() it becomes an asynchronous So, which means it will take this asynchronous and it has to wait for everything on a stack to finish So, console.log b and c is going to be in the stack and it would execute that one first
Then all of the asynchronous would cut through the event loop and then it would get executed I do have tutorials, you can check it out. The last question is “What is closure and how do you use it?”
Pretty much I would say 90% of the interviews that I have been to They have asked me this question So, the answer is When a function returns another function the returning function will hold it’s environment basically all the variables is needed it’s environment
Alright, to explain this what I will do is I will create an object equaled which is a function And it will have a private variable Let i = 0 So this will be an environment this function Right? And, All I will do is return an object with two functions
The first one is setI() which takes an argument let’s say ‘k’ and sets i = k and it has another function called getI() Which simply returns this So, this object has a variable I and it returns these two functions When I return it I will get this object
But what it will do it it hold the environment, which is i inside of it When I execute it outside So, let’s say if I do like x equal to object if execute it, it returns with this object out right let’s say if I set i set i equal to 2
And if I do console.log x dot get I it should return me 2 so when I run this, it returns me 2 then I can keep…i can set a different value let’s set four and it will return me 4 And if I look at the entire object I would see closure
So, if Iook at the object I would see scope and inside there is a closure called 4 similarly this function will also have a closure for 4 So, each of this function can carry it’s own closure so this is how it holds it’s variable inside.
If you want to know more about closures I have a tutorial provided in the link here. So that was it. But, besides all of the questions My advice would be when you go for a JavaScript interview Or any interview for that matter When you are asked to solve some problem or question
Don’t just jump. Uh, it’s human instinct to just get over things then you just jump into it and then try to answer That is the wrong way. Because what happens Sometimes the question is tricky and it’s a trap It’s an obvious question but it has alternate answer
So, you need to understand what the interviewer is asking. So, what you do is you get the question You, give yourself a minute. Try to understand. And also if you have any questions Ask them. “Do you mean by this or that?” Try to clarify. This shows that …uh… you have proper thinking process
Once you do that then start by planning how you are going to answer this question in your head. And then answer it properly if you have to then…let’s say do a coding solution then you wanna say things every step of the way what you are trying to do
Rather than just start coding and that was ok to fail I have failed a few interviews in my days and also the more interviews you do the more practice you have the more understanding you have so you get better and I would love to hear your experiences
And some of the questions you’ve got So I can share them with my audience Sharing is also very important So, if you like this tutorial Like it, subscribe And please provide any constructive content. Thank you.
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
my first js interview went terrible as i didn't watched this video but will go for more as it gave me some nice experience
The channels name is On Point
Arrow function = Stricted encapsulation
I wasn’t aware that Closure was an actual property that can be accessed. Thank you for showing me this!
Phenominal! Thank you!!
Around 04:10 I'm 99% sure I understand what you're getting at but an example would be really helpful to solidify the explanation
Just hang up if any interviewer asks you what's a Closure.
Very useful. Thanks
When check out this channel because you are a star wars fan: 😢
When you remember you are a web developer who has an interview later today: 🤔😁
Great work sir 👏👏👍
4:10 I'm not sure why you said in the video we can't pass function declaration into another function? If we declare a function we can pass it as an argument to another function.
Sir Great video but please improve the Audio Quality.
Thanks for this video. Yes, closure is a very important topic of Javascript. I have faced it two times.
Just wanted to comment that you CAN pass a declared function in as a variable. You give the function a name in the declaration. So in his example, you'd be able to pass funcD into funcE (if funcE accepts arguments), so something like…funcE(funcD) would console log out both strings (again…if funcE calls the argument passed in)
Thank you for valuable stuff 🙂
Function declaration and expression can pass to another method
🎉🎉🎉
Thanks, man, you showed the best example for closures understanding, nice job!
8 out 10 questions were asked in my interview, after listening to your tutorial , i am pretty confident about everything .thank you so much gurujiii, all the best!! great going !!!
If we use var instead of let in function expression, hoisting will take place and will show no error.
Thanks sir 👍
I have an interview tomorrow and mostly about javascript. I have a feeling that I will fail it but many thanks that I found and watched your video. Very helpful!!
this video series is awesome
Awesome tutorial. Thanks
4 years ago, in this is still relevant
Sir Can you please help me to understand below function ?
const addImpure = (() => {
let state = 0;
return (v) => {
return state += v;
}
})();
addImpure(1); —> 1
addImpure(1); —> 2
addImpure(1); —> 3
Thanks in advance
Thanks a lot for feeding us with the Javascript knowledge and preparing for interviews.