Top 25 JavaScript Interview Questions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends nowadays full stack development is very very much in demand and everyone is asking what should I learn for UI development react or angular the answer is none of them first learn JavaScript you do not have to be very good in it but yes basic Foundation knowledge of JavaScript is very necessary if you are from some back end programming background like c-sharp Java python then it is going to be very very easy for you same for each arrays list classes objects okay and if you are new to programming then this should be your first language to learn the reason is simple JavaScript is the most popular language in the world and we use JavaScript in ninety percent of the web development project and in every every developer resume Java Script should be there so in this many codes I am going to cover top 25 interview questions in JavaScript in first chapter we will explore the questions on basics of JavaScript these are like short answer questions so it will not take much time okay then in the second chapter we will have questions from functions function and its types I think it is the most important part of the JavaScript and then in the third chapter we will cover some Advanced questions like promises classes and objects in JavaScript as you can see the questions are are here in the sequential manner sequential learning so that you can learn step by step okay now here is one very important Point why I am able to complete so many questions in so less time here because mostly I used screenshots to explain the code and the output I only focused on clearing and explaining the concepts if you have any confusion then type this code in between the script tags of your HTML file you will get the result and you will that is the right way to learn the things right all right so let's start with the first question of the first chapter hey my name is happy and I help candidates in cracking technical interviews what is Javascript let's explore here are our browsers which we use to access websites as a user now our browsers understand only html.js and CSS nothing else HTML and CSS are purely for displaying the data they just display static content nothing else now as a user when you will click on some button or take some action on web page then it is Javascript which is making the website interactive and dynamic how the web page understand JavaScript because every browser have JavaScript engine in it and process which process JavaScript code like V8 JavaScript engine is there inside the Chrome browser spider monkeys for Firefox JavaScript core for Safari and chakra for Edge so what is Javascript JavaScript is a programming language that is used for converting static web pages to interactive and dynamic value Pages that's the answer what are data types in JavaScript what are primitive data types for example here we are assigning a number value to a variable right so react will oh sorry JavaScript will automatically identify by looking at the value that the data type of this age property is number okay the definition is a data type determines the type of the values that can be stored in a variable now these are the types of the data types which are divided into two categories primitive and non-primitive what are primitive data types here is the list of code for all primitive data types okay number string Boolean undefined all are non oh sorry primitive data types basically primitive are simple types and it means once you assign a value to a variable then it will never ever change for example here you assigned 25 to this age variable okay so in the memory your 25 value is stored and assigned to this a variable this memory is managed by the JavaScript engine which by default comes with the browser right you know now when you assign another value 30 to the age variable then a 30 will be stored as in at a new location in the memory and that location will be assigned to the edge variable now okay so basically we are not modifying the value of first location and converting it from 25 to 30 but we are creating a new value here in case of all primitive data types so the conclusion about the primitive data type is primitive data types are immutable which means that their values cannot be changed or modified once they are created and another important point about primitive Vector type is they can hold only single value you can see all of these primitive data types um here are holding only and only single value right that is the answer of this question what is the difference between primitive and non-premitive data types let's see the difference between these two categories of data types then you can use them in a better way okay first obvious differences number string Boolean undefined null all these are primitive data types they are in one category similarly object array function date regular expression are non-premitive data types they both are fundamentally different categories here you can see one object which is the type of non-primitive data type okay now the first fundamental difference between them is primitive data type can hold only single value but non-primitive data types can hold multiple values and methods we know a number string can hold only one value right but if you see this object here which is non-primitive data type then see it is holding multiple values name age etc etc and it can also hold an array even a method like this so this is the ability of non-primitive data types okay next difference is print primitive data types are immutable meaning their values once assigned cannot be changed but non-primitive data types are mutable and their values can be changed for example if you update the name property of this person object here then in the memory the value of this name will be updated and modified no new memory new no new memory place will be created for but I already explained in previous question how you create new memory space when you assign a new value to same variable in case of primitive primitive data types right next difference is primitive data types are simple data types but non primitive data types are complex data types why we call them complex because they are more flexible as comparison to primitive flexible instance they can accept multiple values their values can be changed so you can do multiple things with the non-primitive data types for example in real world TV is a simple thing because TV you can watch only right but computer is a complex thing because computer you can watch browse Place games so it provides you more flexibility right so that is the difference between primitive data types and the non-primitive data types what are arrays function and objects all right here we are going to see the three main data types of non-primitive types first one is array see here is the way you can assign a array to a variable but is that a simple and array is used to store a collection of values such as a list of numbers or list of names list of list of something right like here we are using this animals array to store the names of the animals right now like this with the help of this index we can access any item or element of this array array index starts from 0 then 1 2 3 so here we passed index as 1 so the output will be cat next thing about array is length which is the count of the total number of elements in the array here we have two count right next important thing is you can add the new elements inside the array with the help of this push method like this and if we print the final animal list then after pushing you can see here we have now three animals right so this is all about array next important non-primitive data type is function here is a very simple function add which is accepting two values as parameter and then returning the addition of the two very simple right I think you already know that if we execute this function then we will get the value 5 here simple the definition is a function is a block of code that performs a specific task or returns a value so a function may or may not return a value okay third important non-in primitive data type is object here is the example of object person you can see we can assign multiple values to a single object person here like this these thing we call properties even we can assign an array to the property of object and even we can assign the function also so this is the beauty of the object now this is the way to access the normal properties of object by passing the property name for array properties we have to pass the property name and the index also like this and we can directly call the function of the object like this so finally the definition of the object is an object is a non-primitive data type which can hold multiple values or a combination of values and function that's it what is scope in JavaScript if you see this code of my function here inside the function we defined one let variable and assigned a value to it and then print it in the console everything is fine until here but when we try to access this let variable a outside this function body like this then it will give the error this is local scope local scope means variable declared inside a function have local scope which means they can only be accessed within the function not outside now in second case we Define the variable outside the function and inside we print it first manipulated it fine then outside again we logged it okay and it will lock fine also because of the global scope in global scope variable declared outside any function have Global scope which means they can be accessed from anywhere within the within the function or program so that is the global scope that's the answer of this question what is the difference between where let and const in JavaScript all right to show the comparison I will show the code side by side let's first see the code example of where keyword here we have one function example and inside it we have an if condition which is always true inside this we are using the where keyword to assign the count to a number value 10 10. when we print the count inside the if if block the output will be 10 which is expected okay but when we will print the count in outside the if block even there it will print 10 out as output like this the reason is where creates a function scoped variable even though you define the where variable inside the if block still it is available outside the if block because it is function scope not blocked scoped okay if is a block but this where is available to hold function okay then another way of defining the variable is let keyword here is the same code just I replaced this letter a keyword with this sorry this where keyword with the Led Led keyword here this let variable will work inside the if block only but if you will try to use this variable outside the if block then you will get the count is not defined error okay because let creates a block scoped variable therefore the variable will be available inside the same block only and not the outside so that is the difference between them mostly we prefer let keyword only because it is uh what mostly everyone preferred because it is more logical right next one is const keyword which purpose which purpose is slightly different uh here in the code you can see we assigned the value const Z is equal to 10 and then we are trying to assign a new value to Z then it is throwing the error the reason is const cannot it can be assigned only once and it value cannot be changed afterwards all right I hope now you know the differences between all these three keywords what is loop what are the types of Loops in JavaScript now let's quickly see loop as a diagram see when Loop starts there must be a condition then and if the condition is true then code inside the loop will execute and counter will be updated again if the condition is true then again code inside loop will be executed like this is a iteration process again and again and again that's why we call it Loop right but once the condition is false then the execution will exit exit from this Loop and program will stop simple right definition is a loop is a programming way to run a set of instructions or you can say code repeatedly until a certain certain condition is met in JavaScript we have five types of Loops for for Loop while loop do while four off and for n what is the difference between for while and do while loops first let's see the for Loop code in one C1 in one single line we are defining the initial value of the variable then setting the condition also and then incrementing the counter also all in one one single line right and inside we are just printing logging the value and here is the output so what is for Loop for Loop allows to iterate a block of code a specific number of times okay specific number because you must set the increment here or you must set the initial value of the number also now if we will see the while loop here defining the initial value of a variable then setting the condition and condition with while keyword here and then incrementing the counter at the end inside the loop so all is app happening in different lines right but here in the while loop if you do not have initial value and you even you do not have the increment and only you have the condition only then for you while loop is a better like then you should use while loop so the definition is a while loop execute a block of code while a certain condition is true it's depend on the condition only in real application if you have the initial value condition and you have to increment the value then you have to better use the for Loop but if you just have the condition only then you should use while loop right and the last one is do while loop it is almost similar to the while loop only one difference is if this is the code of do while and you can see see value of K is equal to 2 oh sorry 0 but our condition is if K is greater than 1 which means our condition this condition is false because k k is 0 and if it is while loop then it will not execute but for the case of do while loop it will definitely execute the first time only and we will get inside the loop and the output will be locked like this but from second time onwards it will execute as per the condition like while only so the definition is the 2y Loop is similar to the while loop except the block of code is execute at least once even if the condition is false so that is the comparison between these three loops what is the difference between four off and foreign Loop four of loop is simple version and here you can see the code we have an array of values like this and then we use far off Loop to iterate through these arrays of values like this and here is the simple output so the definition is the far off Loop is used to Loop through the values of an object like arrays strings okay now if you see the foreign Loop code here we have this object person with properties like name age role with their values right it's like a key value pair right so to iterate these key value pair these properties we need for in Loop so here we will create all properties inside the person of it sorry not create it's iterate all properties inside the person object okay now another difference is far off Loop allows you to access each value directly without having to use an index whereas foreign Loop allows you to iterate over the keys of an object and access the values associated with those keys those properties okay now you know for arrays you have to use for off Loop in real application and for things like objects you should prefer a foreign Loop which is like used to uh tackle more complex objects what is for each method compare it with far off and foreign Loop so for each is a method which is able to do both the things actually which far off and foreign can do that both the things you can do by for each also for example if this is the array then this is the far off Loop to iterate it right the same array can be iterated using the forage Loop like this also and we will get the same output here what we are doing here array dot for each method will accept a function as a parameter which will get all the items or elements of array one by one as the parameter here and therefore logging one by one these values so this is slightly more convenient and and it is good for performance also because we do not have to declare one extra variable like item like we were doing it with the for loop loop here the item here is the parameter not the variable okay so next uh the comparison next one is comparison with the foreign here you can see one person object here is the foreign Loop to iterate the properties with the help of the key value pair right but the same can be achieved with the with the help of the for each method like this all the values of object person will be iterated using the for each method and hence all the values will be printed like this if we will replace this object dot values with the object dot Keys then all the keys will be printed locked okay got it so for each is a more more modern way to iterate through any list okay whether the list is an array or object that doesn't matter so finally the definition of the forage method is for each is a method available on arrays or objects that allows you to iterate over each element of the array and perform some action on each element that's it I hope it is clear to you what is the difference between double equal to and triple equal to in JavaScript as always let's see the code example first see here we have two variables X and Y access the number and Y is the string now if I will use double equal to operator then this will evaluate this to be true and this will be the output so you noted here double equal to operator comparing irrespective of the data types it doesn't matter whatever is the data type okay that means double equal to operator performs type question okay and converts the string to the number and then compare now if we will use the triple equal to com for comparison and then this will be the false now the reason is triple equal to does not perform any type coefficient and only set to true if value and types both are same so here this not double equal to will be true because the types of X and Y are not same mostly we use triple equal to because we do not want to values from different data types to be equal right that is not logical so that is the answer of this question all right congrats for completing the first chapter of this mini course which had the basic fundamental question now in this chapter we will cover questions about all the types of functions in JavaScript Anonymous functions Arrow function callback function high order function yes they are very very important to understand and yes if you are new to JavaScript then do not forget to try this code in a simple HTML files just place the code in between the script tags of body or head of the HTML and see the magic then so without wasting time let's start with the first question what are the types of functions in JavaScript all right so yes we have seven types of functions in JavaScript named function Anonymous function expression Arrow function double I Fe callback function and high higher order functions so these are the seven types of function we will cover all the types of the functions in separate question in this question I will explore the first two as they are the simplest one okay so this is the functional declaration or we call the named function also this type of function because we assign a name sum to this function like this we are calling the function with function name directly inside the end then inside the console.log right here so it is the simplest one no explanation required right the second type of the function is anonymous function you can see the code here how inside the console.log there is the function body but there is no name assigned to it Anonymous function means Anonymous means what no number right therefore function with no name is called Anonymous functions and you will be using Anonymous functions at many places in your JavaScript applications wherever you see function keyword and then bracket start without any name before that that means you are using the anonymous functions okay so that is the answer of this question foreign expression in JavaScript if you see this code example here we are assigning the anonymous function to a variable ad right and then we are using the variable in console.log to call the above anonymous anonymous function this is nothing but function expression the definition is simple a function expression is a way to define a function in JavaScript by assigning it to a variable now instead of anonymous function we can also use the named function also to create the function expression like this again the named function is assigned to a variable and then we call the function with the variable name and passing parameters this is the second case but we rarely used named function with function expression mostly we use the anonymous function only with function expression okay because we are getting the result of the function in a variable because you know there is no use of in if you see this named function then there is no use of this sum method name okay so why should we should put it so that's the reason why we mostly use anonymous function Expressions only that's the answer of this question what are Arrow functions in JavaScript I think this is one of the most important function in JavaScript if you see this it is a normal traditional named function right this is just a four to five lines of code Now by using the arrow function we can do the same thing in just two lines of code like this now listen carefully this ad is the arrow function which is accepting two parameters X and Y inside the brackets the same parameter as you were getting in the normal named functions okay these are nothing else but parameters then after this Arrow operator you will write the logic like whatever the code lines of code you will write okay in this one line of code Arrow operator will implicitly take care of returning the result without writing the return keyword so if it says one line code then you and you are right returning something then you need not to write the written like you were doing in this named function now if you will put X Plus y this statement inside some curly braces or you have the multiple statements then you have to write the return statement okay so finally the definition of the arrow function is Arrow functions also known as the fat Arrow functions are a concise and short Syntax for defining functions in JavaScript that's the answer of this question what are callback functions these callback functions are very very important and used everywhere in the JavaScript so let's explore them here you can see we have a simple normal function add now we can call this add function by passing two parameters and then get the result in result variable right here name of the parameters A and B are not necessarily same as the name method actual parameters okay now we want to put this console.log inside some function to make this program as a good structured program right because this is right now scattered so what we will do for that we will first call one method display which will accept two parameters values and third parameter this is as the method name add is our method name which is passed as an argument in the method another function write display so this ad is the Callback method which is passed as an argument the definition of the Callback function is a callback function is a function which that is passed as an argument in another function now here is the body of the display method which accept X and Y and operation which is a callback function in the parameter okay so remember here operation is the Callback function and displays the higher order function which I will explain in upcoming question in detail so inside the display function then we can call the operation function which is like add right now then result will get the sum of the X and Y and that will store in result variable and finally log the value here is the output 8. the question is what is the advantage here okay why we use the Callback and at the higher order function why can't we just do all these one single normal function can also do is do do this simple thing right the reason is one function add is fine but suppose you have multiple functions like multiply subtract divide and many more then this code will not look will will this this higher order and callback function will look more clearer and structured but if you do the same one thing in a single function like we were doing previously here then this will be like very untidy and unstructured and not the clear code okay so that that's why we use callback function at many places in our applications in JavaScript that's the answer of this question foreign when to use callback functions in real applications callback functions can be used in many places and here is the list you can use callback function for iteration event handling asynchronous operation higher order functions which I already showed in the previous question right how to use higher order function and the Callback function together and yes also with promises also you can use callback function now you know how important our callback functions right this simple entry iteration uses like this I will show it here suppose we have an array like this and we also have method which accept a number as parameter and then log the number simple now how to call this method for that we have to just use this for each method of the array which accept callback function a log number as the parameter and log number will automatically accept one by one all the numbers or numbers of this numbers array okay and then log them simple whenever whenever you see a code where method is passed as the parameter or you can say argument in another function that means that function is a callback function which is passed as an argument we will also discuss the other ways of other uses of callback functions in upcoming questions foreign handling in JavaScript all right suppose you are a user and you open a browser a web page and in the web page whenever you click some button or or do some action on web page that what we call it we call it event sorry and here is the list of top 10 events like click event Mouse over key down event etc etc now when this event will be triggered or happened then you can write some code to handle it as per your web application okay and not as an user but as a developer only you have to write the code for example here we have a button on our on our web application with ID my button now we can get the reference of the button by using this code document.getelement by ID method of JavaScript right now we have the button reference with us so after that we can call this add event listener method of this button reference add event listener normally takes normally takes past like two parameters inside it first is the event name which is click here and second is the logic or function which we want to perform or execute on button click okay in this function we are just calling this alert now if you can see here the past function as the argument of this add event listener method right so this is a callback function because it's a parameter argument right also this function has no name so this is also an anonymous function okay so don't confuse that whether it's Anonymous and whether it's a callback function it's both right because it's a parameter and also it has no name and yes we can also pass the named callback function like named also callback function also so if the logic is too big then we normally prefer named called callback function to use so this is the a story about the event handling uh now let's summarize the points uh event handling is the process of responding to user actions in a web page first point second point is the add event listener method of JavaScript allows to attach any event name with the function you want to perform on that event okay I hope now you know what is event handling and how callback function helps in event handling what is higher order function in JavaScript all right we call a function a higher order function if it takes one or more function as arguments or it written a function as a result so one of them will be true then it's a higher order function so let's focus on the first case see here we have function Hof which is accepting Funk as a parameter and then simple cowling Funk inside it here h of Hof is the higher order function and func is the Callback function because it is passed as the parameter okay now this is the way we call the H of function and pass the say hello as the parameter I explain the use of this type of code when I explain the Callback function in the previous questions right we can write many methods like say hello and pass them as parameters like it's not just a single parameter uh callback function we can pass multiple methods as the parameter also in case of a higher order function so this will be this will give you a better structure of the code now second type of the higher order function is where a function returns a function as a result here we will not use callback see this function create Adder function is higher order function because inside it we are returning the result of this function which is just adding the value of the numbers and yes this function is an anonymous function but yes you can name it also so now the question is how you will use this create Adder for higher order function what is the use so this is a two-step process first step is we will pass the parameter 5 in higher order function and get and store the result of the function result in this add 5 variable so this is not a just a local it's a function variable and as a as I said higher order function return function as a result and now this add 5 is the function result then we will call add 5 function and pass the value to the log pass the value like this and log the final thing okay so the output will be 7 which is basically five plus two so this function as the result is not a very like this is not high this case of higher order function second case is not a very common use like it's not used commonly but but this using callbacks with higher order function is used at many places in normal JavaScript applications great and congrats for completing the second chapter if you are here means you are serious about learning and interviews right in this third chapter of this mini course we will cover some advanced level questions on the JavaScript not many but some like promises classes objects so let's start with the first question of this chapter what are asynchronous operations in JavaScript let me show a very simple code example after which you will know what is asynchronous operation for example see this piece of code where first we are logging some statement and then we are using set timeout method to log another statement here set set timeout method has two parameter first is the Callback Anonymous function and second is this the time okay this means this callback function will automatically execute after this period of time okay so second very important thing about this set timeout method is this method is asynchronous that means if we will write some uh write one more statement after this set timeout then this will be the output so what what happened here here first before set timeout method will execute then add the second line this set timeout method will wait for to 2000 millisecond or you can say two seconds to execute this callback function so this function will be executed after two second but but but this set timeout will not block the execution the further execution so again this Set uh so what will happen it will let the execution continue and therefore this next lines after set timeout will be logged first and then when the two seconds will be completed then inside set timeout will be I also be executed so this is the way of execution will process the thing faster in asynchronous mode because blocking is not there okay so this is what is asynchronous Operation okay now let's summarize this uh asynchronous operation or operations that do not block the execution of the code set timeout is the asynchronous function which is executes callback function after a specific period of time so that is the definition of the set time set timeout method which is used many many places in JavaScript Apple applications now if someone will ask you where we use a synchronous operation in real applications then these are the places where we can use asynchronous operations first as I already showed you set timeout is asynchronous example then we have promises then most importantly we when we are loading data from the API then we also we are using a synchronous operation uploading files animations and transition transitions all these we will use asynchronous operation and why we use a asynchronous programming in these operations because all these operations are very time taking and we do not want to block our application execution when we are calling an API or uploading files or running some animations or transition which is time taking right we do not want to wait the program for their completion okay so that's the answer we will explore all these operations in upcoming questions what are promises in JavaScript this is a very important question in JavaScript we will first see some points about promises and then while checking the code code example we will confirm these points okay so first point is promises in JavaScript are a way to handle asynchronous operation second point is a promise represents a value that may not be available yet but will be available at some point in future okay for example you call one API method then might be the data is not available yet right it will take some time three four five ten seconds but it will be available in the future right maybe no data some error you will get or some responsible gets but at least something you will get and the last point about promise is promise can be in one of these three states pending result and rejected now let's first see the sample promise prototype see here we are creating the promise with the help of This Promise Constructor and assigning the result to this variable okay inside the promise Constructor in short we are doing three steps first performing a synchronous operation then if the operation is successful we call the then we are calling the resolve callback which is passed as the parameter so basically if the we will get the result positive result from a synchronous operation then we are marking it as successful and resolve will handle it okay and if the operation is failed then this is the last step we will call the reject callback function so this resolve and reject are the Callback functions and these are the three total steps like high level steps that's it okay that's the answer of this question in next question I will show how to actually implement the real example in the promises how to implement promises in JavaScript in this question I will quickly show you the simple implementation of a promise example for example here we are creating the promise and assigning the result of the my prom to the my promise constant okay now instead of getting the result in this constant you can also enclose This Promise in a function okay so that is another way okay remember you do not have to write the body of this resolve and reject callback functions okay you will get them as the part of this promise Constructor only now inside this we are using this set timeout to make our operation asynchronous remember the first first step to make the to call the asynchronous operation now here after 1000 millisecond the things inside width will be executing so first we are generating here a random number between 0 to 9 and if number generator is it is less than 5 we are marking it as success and call the resolve callback function like this and if random number is greater than 5 then we considering it as a failure and then we calling the rejector Callback function like this so in real applications also we put API hitting mostly we are like hating the API and if we get the data successfully then we resolve it and in case we get the error we reject it so our promise is now ready to run a synchronous operation and handle success and failure cases now the question is how you will log the success of this error information so here we are not writing anything in the log right so for that we have to use one more method of promise and that is than the function that is the then function like this this then function will receive the result passed by the resolve method of the promise and then it will log it similarly the sketch method of the promise will receive the result passed by the reject method of the reject method and then it will log it so you got it now if I run this program then this will be the output since here I used random number method therefore sometimes value will be less than 5 it will then if we log the result sent by the resolve method and sometimes the value is greater than file 5 which means error then we log the result sent by the reject method so every time you reload the page sometimes it is success and sometime it is error and that's it this is the Practical and theoretical information about promise you can also try the same code in your HTML file just by keeping the code in between the script tags and of the head or body section okay if you will try it yourself then only you are going to learn if I will code it then I will my coding speed will get better but you will not learn then but if you will do it then you can learn it easily when to use promises in real applications so every Concept in JavaScript has some real application use okay I hope you remember these three point about promises now if I summarize them in one line then promises are useful in doing a synchronous operation and handle the results when the result will be available in future okay and here are the cases when you can use promises in your application for API calls file handling data fetching animation visual effects and event handling for example you can use set time set timeout or assing you these are all the real application use of the a synchronous programming also so you can use set timeout to do all the things in an asynchronous operation right but then how you will manage the result or response which these operations will give you for managing the response only we will use the promise okay so it's like they are working together so asynchronous operation set timeout promises callback functions all they work together okay and as the name suggests promise will not give the response may not give the response immediately but it will surely give you the response whether it is resolve or reject after some time okay so that is the meaning of the promise I hope you know now you know how what is promise how we can Implement promise and what is the and where are the cases where we really can use promises in our real applications what are classes and objects in JavaScript so normally we have SIMPLE variables like this in JavaScript right but for complex data types sometimes we have to Define classes like this with the help of class keyword okay here car is our class okay and then we can have these static properties with fixed values in car inside car then we can have Dynamic properties also like this which we will receive via the parameters of the Constructor of the class okay I will show you I will show you later in this question only so these are the dynamic properties and then we can also have the methods like this in the class now this class is just a template or you can say it's a blueprint it will not do anything by itself so the definition of the classes like class is a template for creating objects with similar properties and methods so it means to use a class we should create the object of the class okay so and this is how we can create the object of the class and pass the parameters these parameters will be received by the class Constructor here it is model and year and then the class properties will be set with the help of this parameter received okay so class properties are set by this parameters okay so why we call them dynamic because here we can pass anything at that like for example this is a car class so you are creating object like BMW then the model will be different the year will be different you will grant Mercedes a model will be different so this is like Dynamic code that's why we call them Dynamic properties but these static property value it's its value is fixed we cannot change them because every car must have four tires only so so it depends on your requirement how what properties you want to set as a static what properties you want to set as dynamic okay so after we can use the class my object to lock the property values like this you can even call the methods of the class from this object like this okay so object is is the way to reuse the class otherwise your class is of no use okay so the definition of the object is objects are the instances of a class which are created using the new keyword I hope now you have some basic idea about what is a class what is the object in JavaScript right what is the purpose of this keyword in JavaScript now if you remember our class code and here it is here we used this keyword inside the Constructor Okay the reason for using this this keyword is suppose you will not use this with the model at left then model equal to model will be confusing for the program okay for the compiler which one is the parameter and which one is the Class Property how the program will know on how it will decide so to resolve this problem we use this this keyword with the with this this keyword with class properties when you say this dot it means you are pointing or referring to the properties or the of the current class car or context okay so that means this dot model is the property of the class the car class and the model at the right side is the parameter received from The Constructor okay so the answer is this refers to the current constant or account context or scope in which code is being executed in this example in this example scope is the car class is the scope okay so as I said current is context or scope so car is the scope here what is hoisting in JavaScript for example see this code here function is called first and then we are declaring and defining the my function so the question is how is it possible to call the function first even before declaring it and this should give an error right so but this is possible because of hosting in JavaScript when you will run this code the JavaScript engine will automatically move the methods declared to the top of the scope so code will be rearranged and then it will be compiled okay so it will work now one more example is this code where a variable is declared at the end and it will also work and it will also give the proper output again this is because of Hosting for variable it will move the variable declaration to the top of the scope at the compile time so this will work also the definition of the hoisting is something like hoisting is a JavaScript Behavior where functions and variable declaration are moved to the top to the top of their respective Scopes during the compilation phase okay I already scope uh explained scope right in my previous questions now if you see this code this is like a query okay here the output will be undefined now what is the reason what is the reason the reason is because Declaration of X will be moved to the top but the assignment assignment to 10 will not so remember declaration is hosted not assignment so that is the answer of this question congrats for completing this mini course let me know in the comment section if you want the Practical of these questions or not or if any extra questions you want to learn at the end I would like to say all the best for your career and if you are preparing for interviews then my only advice is keep giving interviews and never ever give up my best wishes are with you are always with you
Info
Channel: Interview Happy
Views: 13,348
Rating: undefined out of 5
Keywords:
Id: OEQpRTXfxoo
Channel Id: undefined
Length: 50min 26sec (3026 seconds)
Published: Wed May 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.