Basics of Programming | Ep 3 | Node JS Tutorial For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to AR tutorials this is nodejs full tutorial series for absolute beginners in the last episode we did the installation of nodejs and we made sure a basic simple example we wrote to test that our code is ready and we are good to start writing code before becoming a nodejs developer there are some important concepts of programming that is very very essential that is very very fundamental in order to learn and become a nodejs developer the programming Concepts that I'm going to cover in this episode will not only help you with nodejs but in general of programming with JavaScript or any other language what all I am covering today but before I start talking about the things we are going to cover today this is going to be an exhaustive uh series that I'm undertaking I would definitely need your help and love with the likes um so this is going to to be a 50 part series including nodejs expressjs expressjs with mongodb expressjs with mySQL and around 50 episodes I'll cover in this series so please do hit that like button to keep encouraging and motivating me thank you in advance so what are we going to cover today I'm going to cover top 12 um programming Concepts that are absolutely uh critical important if you go through this episode uh correctly and and in in completely entirety I'm sure it would help you with JavaScript it would help you become a better developer in general so these are the top 12 topics I'll cover today I'll teach you how to write classes in JavaScript functions Arrow function Global objects understanding this keyword async programming call back promises async await handling errors promise chaining fetch handling and error handling in promises right so these are the top 12 uh basic programming things I'll cover in this episode I'm sure you would love and you will appreciate my effort that I'm putting in all right without wasting any time let's jump right into code we did the installation of Visual Studio code editor in the last episode and we wrote a simple console log in the last episode so I will cover the same I'll create some uh new file today let's close this I'm in node.js tutorial and first I'm going to clear class tutorial that's what I'm covering first and I'm going to write a quick class and I'll show you how that works so you write class followed by the name of the class now it can be anything it can be car something you can write a Constructor in it and let's say make and color these are the two things we pass this dot um make equal to make this do color equal to color so all we are doing is is assigning some values okay and then we'll write um say a method where we will use these two things and do a console log and say um we can you can just do a console log and say this do make comma and you can you can or you can also use a template literal right um that way it would be much easy so I can just write this followed by curly prce and we'll write this dot color and then we can also write dollar this dot um color okay so it would print both of them in one line so this is a classic example simple example of a class that I can think for you followed by the name class is a keyword you have a Constructor and then you have a simple method inside it how do you call it you can just initialize by a new object Okay so we can say my car equal to now this can be any um thing that but in our case the name of the class is car and you can see it is taking two things so you can pass and the color right you can choose any color that you would want and the last thing we can do is my car dot call that method which is display info so this is a simple example of a class and an object so this piece is a class this is a new object is an object of the class instance called car all right with the instance name is my car and I'm just calling this method which is inside the class this is one of the most basic things that you should know when you working with nodejs or in general with JavaScript also so how do you run it you write type node followed by the name of the file now you see here it is printing Audi red and that's coming from this method but this method is also taking in Constructor the values from the Constructor so this is a first example that I want to show you of a class and an object all right now you can modify this into a person user whatever accounts you can write different uh variations of you know uh class that we will work in real time but if you are new here just type this code run it and stay with me and do what I'm doing okay all right the next on my list I have is the functions so functions tutorial. JS I'm creating a new file so that it's easy for you to understand and type along you write function keyword followed by the function name now let's say you can say reset password right now this is a function that I'm writing it will take two parameters let's say email and um say token right that you want to pass whatever in general right uh you can say return um email plus token okay whatever um so this is where you will basically write the logic of what it should do right logic goes here inside the function but the syntax is function keyword followed by the function name and some parameters that it can take and how do you call it you can just call it reset password followed by the value you can say test at AR tutorial.com and token can be any key like this okay so now what happens this will be triggered okay and you can just do a console log also and it would give you that value okay so again to run it you will write node command followed by the file name which is function so you can see here it's now giving a token which is is appending with the email address right so this is an simple example of how you write a function function followed by function name some parameters you call it to assign or to do some operation okay this is second example the Third on my list is the arrow function tutorial so all these concepts are very very important um especially when you work on a day-to-day basis right uh these are something that you would find yourself writing code all right so next is Arrow function here we will not write the typical function followed by function name all that instead short hand notation here we are saying it's a function which will take two parameters and it will return a into B so this is a shortcut of writing a function if you notice here we are writing function keyword followed by function name then parameters all of that instead in Arrow function function this is a shorthand to write a function which will take two parameters if it is not taking any parameter this is also fine all right so in our case it's taking two parameters now let's do a simple console log and call this function how do you call it followed by the function name and pass the value that's it to run it you would write node Arrow functions that's this fun file name and you can see here output is 24 because it's this is the output that it is going to give us all right that's the third uh important concept that you should know which is Arrow function all right I hope I'm not moving too fast uh because I have to cover a lot of things so I'm moving but do let me know in comments if you uh have any doubts on any of these I'll be more than happy to help you all right the next on my list is global objects now these are common objects that there are lot of built-in objects that we can use in JavaScript and how do you use them you can use them directly let's say I'm going to use math math dot now there are lot of methods that we can use I'm going to use math. random now you can use it in anywhere similarly you have date date dot now so it will give you the now right and you also have Navigator dot location oh sorry uh say navigator dot um what is it uh location length or you can say name right that would give you the name of that then you can also there are so many built-in ones that you can use okay uh Navigator date math etc etc as you as we keep working in the series I'll introduce you to lot of things and you can uh get that okay all right so let's run this node Global object. JS it gives error uh it says Navigator is not defined okay let's see why Navigator dot uh let's say geolocation language locks okay so that was the mistake I wrote it caps okay that's why it didn't give but if I just say navigator Dot and I can see all the details that comes up okay so let's say navigator do geolocation and clear this and run it again o Still Still error uh Navigator is not defined I think we'll have to create a new instance of that we'll come back to it later I have lot of things today to cover for you so you can see here I am not improvising anything but rather we will just stick to basic so that we can cover all right so math. random and date.now these are some of the global objects that are used used and we can easily uh use any uh existing um Global classes that we want to use okay if you if I have to show you a quick instance uh let me try one more time before we say we are giving up um what we can do is Navigator do um let's say a simple try I'll do so here uh it would be Navigator dot so we can use it with window dot instead of just so window is a global thing and now I can just say do language let's see this should work uh it is still giving window is undefined okay that's strange but um let's do Navigator oh okay let me go back to my editor Navigator do language and it is still giving error now let me to real quick check it should ideally be giving us the value but we can debug it later as to why this is not happening um ideally it should have given us this value okay let me try console.log Okay we'll we'll check this later I'm not sure why but this should ideally be working anyways let's not waste time let's keep moving and I will fix that up later for you so the next thing we'll do is the understanding this keyword okay so I'm creating a new file this tutorial. JS now let's do the this uh object now in this understand that this refers to the instance to of the element or the object okay that's the context this means whatever reference you are giving so let's say I have a object and let's define an object and say email and let's give some email address and say test at AR tutorial.com and I also have a method um say hello um or say welcome right so I'm creating a defining a method now what this method does is simply call the email okay I want to print email so I'm going to say hello and then this dot you see email is coming up now in this case when I use the this keyword the keyword this here references to this object and it says this means in this object find email which is this value okay and now when I want to print it I can just do something like this person person dot um I can say welcome and now when I call this particular function it would print this value so if I say node this tutorial. JS you see here it is printing the email address the email address is coming from the object but here the reference is this do email this refers to this particular object okay so it can be used in a class it can be used in an object and multiple places okay so as we will progress in this series you will see me writing lot of this keyword all it means is that it is referencing to that particular instance or the element okay remember that now same way I will go back to class tutorial now in this if I want to make some changes I can say this dot you can see here I can use display info right because this refers to now class the entire class not just the object so you can use this operator inside an object inside a class anywhere wherever you want it to reference its own uh Properties or functions okay now like this if you see here we said this do make because it is using this value so similarly this means it can be used inside an object it can be used inside a class I hope it's clear let me know if it's too fast if I'm going too fast if you want me to cover more this more of this particular episode I'll be happy to do that for now let's move on to the next one which is asynchronous programming right now this is one of the most important topics that everybody should know before you start writing the code okay now async now async refers to the ACT that it will be performed concurrently concurrently means they'll be executed parallely okay so let me show you an example first let's start putting a console log and I'm I'm saying start so what this will do when it comes to line number one it would print console.log now I'm setting a set time out and what I'm doing here and I'm saying it will take a method and what we will do is we'll give a 3 second delay and then we will do a console log and say inside set time out okay and then we are going to say console. log end okay simple I have a console log at the start I'm dealing using set timeout and then I'm writing end now see the execution sequence that's very very important for you to understand okay now node async tutorial. js it says start end and then it says inside set time out that means it did not wait see that it did not wait for this to complete it executed it was executing in the background it executed this this is called async programming that means it will not wait for it okay they will be performed concurrently that means parallell it came here then it triggered this and then it executed this then it went back here that means it will not wait for certain thing and that's why these are called asynchronous programming okay from top to bottom it is async in this case we can however make it synchronous by making it explicitly weight and those things okay we will learn as we progress but understand the sequence of the meaning of asynchronous is that it will allow tasks to be performed concr and that's the beauty of it so you don't really wait on everything instead you can keep doing multiple things at the same time all right the next on my list is the thing call back and event Loop let's do that callback tutorial. JS okay so for this I'll keep it again something similar okay very simple Now call back is nothing but once you get a call back from a function so I'm saying function followed by function name and let's say we have a task of fetching data from the back end and I'm saying call back and here let's do a set timeout and in this again we would just throw in a simple code and we will do console.log data fetched from server so what we are doing we are just fetching a data from server let's say and I'm just doing a call back now call back of this thing that I have passed now let's give a delay here of say one second 1 second and I'm doing fetch data I'm calling that method and inside this I'm passing a function and what this function will do we'll just write call back executed so so far if you notice in the functions we passed a parameter which is a value here you see email token we are passing a value but now I'm passing a call back function itself this is a function which I'm passing here what it will do it would first print data fetched and then execute our call back method which is nothing but a function right so how do you execute node followed by call back and now you see it will wait it says data fetched from server first and then it says call back executed all right so this is a classic example of how you can pass a callbacks function as an argument right here we are passing a function as an argument now this is also a event Loop kind of a thing okay where there are call backs from different functions you can call one function into another you can also pass a function as an argument that's very very important concept because as we start writing Advanced code you would see a lot of these being passed around okay so practice what I'm typing get comfortable get familiar with all the concepts I'm just using basic examples for you not even tough or tricky ones the next on my list uh is a promise so promise tutorial this is my absolute favorite uh thing in um JavaScript so what are promises so promises are nothing but which represent the kind of a eventual completion or a failure okay think of it this way if you have an asynchronous call that has gone it will either complete it will either fail so a true or a false so every time you create a promise especially when you work with Enterprise applications always prefer to write a promise because it's much easy to write a kind of a completion rejection or resolve it right so I'm going to show you an example of how I would do if I have to write a fetch all with promises so let's do a const and I'm saying um fetch data and what we'll do we'll create a new promise and what does a promise do it has two arguments it has a resolve and a reject always always now this is such simple easiest code I think a lot of people think it's scary but honestly it's the most simplest code that I work with now I let's put as a timeout and what we will do after certain time we will resolve that promise now in set timeout we will say after that resolve it and say data fetched from server once you fetch the data you can say it is resolved and here you can close this set time out and say after 2 seconds and you can also reject it if something fails correct that's much easier so you can say fetch data and you can say dot then and you can use the data that we have got here and you can do something like console.log data okay so fetch data here we are calling then right you can also use different variations if you don't want to use do then you can say catch error right and you can also do something like this and say Dot finally right um and then you can also do something like this do error console.log error okay and you can also say Dot finally execute something else right and you can say so there are different variations of how you would use that finally closed this okay so now you can always or you can just do this okay so this is closed this is closed okay perfect so now take a look at this this is a promise what we are doing here we are resolving it and using the fetch data we are saying dot then we can catch it we can do finally but the idea of Promise is either you res resolve like if you can put a condition and say if something happens right resolve or else you can reject right so basically either you will return a resolve or you can return a reject that's the beauty of promise I tell you if you get comfortable with promises you will be way ahead than any developer of your average working environment all right so this is an example of promise right you would see me writing lot of promises as we progress in this series especially with uh expressjs Etc code you would see these things being comfortable so type this exactly what I have written now to run it just type node promise tutorial and executed it will be done so see data fetched from servers finally closed right after 2 second it resolved the promise and finally it closed down okay so this is an example of Promise the next I have is async await let's do that async a wait. JS I think I'm covering a lot of uh programming topics in a very short time I hope you appreciate and I more than appreciating I would really be happy if you also try it with me and get Mastery of that all right so next is async await now what we do here is this is mostly handling of promises especially in the asynchronous operations of promises and you can use it basically for handling of promises right now same thing I'm going to write it in a different way so let's see we'll use keyword async Then followed by a function let's say fetch data and here you're making an API call okay now let's say you make some call and you can return okay and you'll return a new promise now again if it's a promise we have learned already that it will either resolve or it can reject now once it does after certain while let's resolve it so I'm saying after set time out and we will do a console log and say data fetched and I'm using data F because this is one of the most uh common use case right and you would resolve and say data fetched okay so you don't need this here and you did the data Fetch and then let's say 2 second delay and once you are returning this right now how do you call this async function you will write using let's say async function and I have you can only call async function with await okay remember that also fetch data and log let's say and inside this we are going to write constant data equal to a wait fetch data so see since it's a async function I have to use a weit okay if I don't use it I'll show you what happens so these are the just the common basic principles of uh program modern programming languages that you should be comfortable with okay now let's just call it and say fetch data data and log and execute it all right let me just execute and then I'll walk you through it so async a wait. JS it will wait for 2 seconds it says data fetched now if I don't have the await now see what happens it says promise is pending right because it did not wait for this okay it made a call to fetch data but we since we did not wait for for it it went ahead and it is saying promise in the data it is coming promise pending but if I add a wait now it will wait for it for the data to come back now let's see I'll show you one more example with the same lines I'm increasing this to 10 seconds now now see what happens you will wait see it is still waiting till this is resolved it will wait for 10 seconds to get the data same thing happens when you log into Instagram or Facebook or whatever it will wait till your profile is loaded okay so it will not go and execute if this is not completed okay that's why it's very very important to understand the fundamental of async and await every time you use async you have to use await in order to stop that execution and get that data okay very very important fundamental concept if you don't understand this uh it would be very difficult as we write the code ahead okay now the next thing that I want to show you quickly is handling of Errors I think that would be the last on today's episode um I know it's like I said it's too much of programming Concepts but trust me each one is equally important um and each one is equally uh promising in terms of your development as a node ch developer all right um so for handling of Errors right so for that probably I can use um this example itself um instead of going again so for handling of Errors what we can do you can do a try right you can do a triy block and you can do a catch block right so try and catch is one of the most commonly used ones that at least I would be using in my code going forward whenever you would see me writing a try catch you know that why I'm doing that's for handling of Errors okay so this is one of the most common way to handle the uh error handling the other way would be to use a finally okay like we can use uh something like this that we have done earlier I think here so see do catch this is another way of chaining so this is nothing but promise chaining right when you use more than one functions together this becomes your promise chaining that was one of other things here promise chaining so it's sequentially execute methods okay can be chained to execute asynchronous operations sequentially so this is a concept of chaining that means you do first then then you do catch then you do finally okay this is a promise chaining this is one way of catching errors using catch the other way is to use a try catch block and you can do a error handling in this way also okay that's another way now what else what else what else I think that's all I have um think I have covered all the things that are there and please go through this tutorial again and again till you are comfortable writing the code that I have written if you are unable to do so in any of these aspects uh it would be causing little um I'm I would not say problem but I would use the word that some of the code that we will write going forward may be tricky and uses all these things that we have written so far any code that I will write going forward will be more or less on the same lines okay so you should know all of this code by the back of your hand like how I do okay I have tried to do justice to the basics of programming with this 12 with my complete honesty I hope you will appre appreciate my um effort please do hit that like button for me please do subscribe if you like my work please do consider buying me YouTube super thanks in the next episode in this series I will be covering about understanding of modules very very very important very very solid U basic fundamental before you become a nodejs developer thank you so much for joining in this episode I hope you're enjoying I hope you're learning we will continue this series and we will make sure you become a good nodejs developer thank you again for joining see you in the next episode bye
Info
Channel: ARCTutorials
Views: 1,274
Rating: undefined out of 5
Keywords: node js tutorial, nodejs api, node js crash course, Node JS Tutorial For Beginners, node js projects, node js tutorial 2024, nodejs event loop, nodejs microservice, nodejs typescript, node js express, node js backend, node js interview, node js and express js tutorial, node js tutorial for beginners 2024, node js tutorial with project, tutorial node js, tutorial node js express, course node js, nodejs typescript tutorial, nodejs typescript rest api, node js, express nodejs
Id: 18zi5Kocolc
Channel Id: undefined
Length: 32min 37sec (1957 seconds)
Published: Tue Dec 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.