Functions in JavaScript | JavaScript Tutorial in Hindi #11
Aug 15, 2023
Functions in JavaScript | JavaScript Tutorial in Hindi #11
This video is about functions in JavaScript Link to the Repl - https://replit.com/@codewithharry/11f … Join Replit - https://join.replit.com/CodeWithHarry Download Notes - https://drive.google.com/drive/folder … Ultimate JS Course Playlist: • JavaScript Tutorials for Beginners in… ►Checkout my English channel here: / @programmingwithharry ►Instagram: www.instagram.com/codewithharry python, C, C++, Java, JavaScript and Other Cheetsheets [++]: Playlist: • Coding CheatSheets 🧾 by CodeWithHarry ►Learn in One Video[++]: Python[15 Hr] - • Python Tutorial For Beginners In Hind… Python Advance[3.5 Hr] - • Python Programming Course in Hindi (A… Python[1 Hr] - • Learn Python In Hindi In One Video - … Python[2 Hr] - • Python Tutorial In Hindi 🔥 Python[15 Min] - • 15 Minute Python Tutorial For Beginne… JavaScript[1 Hr] - • JavaScript Tutorial C[1.3 Hr]- • C Programming Tutorial For Beginners:… php[1 Hr] - • Learn Php In One Video In Hindi - हिं… php[2.3 Hr] - • Php Tutorial for Beginners in Hindi w… php[Project]- • Login And Registration Form Using Php… HTML[30 Min] - • HTML 5 Tutorial For Beginners In Hind… CSS[8.5 Hr] - • CSS Tutorial In Hindi (With Notes) 🔥 CSS[1.4 Hr] - • CSS 3 Tutorial For Beginners: Learn C… Wordpress[3.2 Hr] - • How To Make a WordPress Website | Wor… Angular[2 Hr] - • Angular Tutorial in Hindi Java[2.3 Hr] - • Java tutorial in hindi 🔥 Web Scraping[1 Hr] - • Web Scraping Tutorial using Python an… MongoDB[2 Hr] - • MongoDb Tutorial For Beginners in Hin… Numpy[1 Hr] - • Numpy Tutorial in Hindi Android Dev[12 Hr]- • Android Development Tutorial For Begi… Linux[1 Hr] - • Linux Tutorial For Beginners in Hindi JQuery[1.1 Hr] - • jQuery Tutorial For Beginners In Hind… Git and GitHub[1.1 Hr] - • Git \u0026 GitHub Tutorial For Beginners I… ►Complete course [playlist]: React - • React Js Tutorials in Hindi Python- • Python Tutorials For Absolute Beginne… OOP Python- • Object Oriented Programming Tutorials… Java - • Java Tutorials For Beginners In Hindi JavaScript- • JavaScript Tutorials In Hindi PHP- • PHP Tutorials in Hindi C- • C Language Tutorials In Hindi C++- • C++ Tutorials In Hindi Git \u0026 GitHub- • Git and GitHub Tutorials In Hindi Android Dev- • Android Development Tutorials in Hindi Python GUI- • Python GUI: Tkinter Tutorial In Hindi… Web Development- • Web Development Tutorials For Beginne… Python Django - • Python Django Tutorials In Hindi Projects Using HTML, CSS \u0026 Javascript- • Projects Using HTML, CSS \u0026 JavaScript… Data Structure and Algo - • Data Structures and Algorithms Course… Follow Me On Social Media ►Website (created using Django Rest \u0026 Angular) - https://www.codewithharry.com ►Facebook - https://www.facebook.com/CodeWithHarry ►Instagram - https://www.instagram.com/codewithharry/ Twitter - https://twitter.com/CodeWithHarry Comment “#HarryBhai” if you read this 😉😉
Content
0.04 -> In today's video, we will see functions in JavaScript.
3.659 -> We use functions in JavaScript to
organize our code.
7.7 -> There are certain code blocks that
may require repeated copying.
12.354 -> And in this case, we want to separate
that logic.
16.933 -> Here, functions in JavaScript perform the
task of separation of logic in this case.
21.711 -> And we can put different parameters
(which are the inputs of the function)
28.363 -> and by applying such functions that,
we can get different results
31.814 -> For example, we can write a function
that will find out the average of number 4.
35.618 -> We can apply those 4 numbers in different ways
38.182 -> and get different outputs from that function.
40.686 -> So, you can say it as function is a black box
that takes input
44.329 -> and gives the output analyzing that input.
48.369 -> This makes the process easier for the programmer
50.32 -> Because he can treat the function as a black box
if the function is written by someone else.
55.264 -> You guys don’t be confused.
Because we are moving on to the computer screen
58.236 -> to see the basics of function and we
will see how to write the functions.
62.469 -> Let's move on to the computer screen.
Let's get started.
71.259 -> So, guys today we are going to talk about functions.
74.657 -> we will learn about functions.
77.6 -> and how to use functions in JavaScript.
81.49 -> At very first, let us understand the
need of creating functions.
85.087 -> Well, when you write a program,
87.61 -> there is a chance that we will use
a few things repeatedly.
92.095 -> Suppose, I need to add a number
with another number.
96.549 -> And I need to do this 10-15 times
in my program.
99.772 -> So, I am better of creating a function.
101.297 -> I will separate that logic,
instead of repeating it multiple times.
106.295 -> Now, repeating is easy, right!
I can copy and paste it.
109.767 -> But if I want changes,
112.156 -> then I can do it only in that
function of mine.
114.584 -> Now, if it is confusing to you,
116.583 -> Then, let me explain it to you by
doing it practically here.
121.5 -> Suppose I have to sum 2, 4 times.
124.977 -> I have to sum A and B once,
then B and C, then C and A.
130.494 -> So, here I will put "let a is equal to 1";
133.232 -> "let b is equal to 2";
135.491 -> and "let c is equal to 3".
139.047 -> A very simple thing!
140.583 -> Now if I need to do the sum here,
143.495 -> suppose I need to write "sum of a and b is".
146.475 -> So, this way we can do it and I
can write "a+b"
151.667 -> Like this way!
153.37 -> And the moment I do this,
156.855 -> "a+b" will be printed.
Very simple
160.355 -> To make things crispy,
I will do average here.
163.49 -> Instead of using the sum, I'll use the average.
165.591 -> Because, sum is very basic.
169.733 -> I will do average of a and b.
171.644 -> After that, average of c and b.
176.358 -> Alright!
177.033 -> We did a and b, c and b,
then a and c.
180.267 -> Now, if I run this program
182.235 -> a very basic value will be printed.
185.3 -> It will print as "(a+b)/2", "(b+c)/2",
188.48 -> and then "(c+a)/2".
Alright!
191.043 -> So, that was very simple.
There was no rocket science.
195.188 -> So, there's no big deal in finding
out the output for this.
198.5 -> Suppose I am doing this work again
and again.
202.042 -> Like I am doing (a+b)/2,
which is a very simple logic.
205.658 -> But I could have done this another way,
208.24 -> which is "(a+b)/2" and add "1"
with it like this way.
212.09 -> Suppose, I want to calculate the average including "1+"
215.533 -> Calculating the average including "1+"
is also easier.
217.764 -> I will write "One plus average";
which means adding '1' with average.
223.033 -> So, it should print 1.5, 2.5, and 3 here.
226.264 -> it is printing 1.5, 2.5, and 3 here.
228.324 -> But I want to separate this logic.
230.201 -> Because I am using this repeatedly!
231.544 -> So, I don't want to write such
complicated thing again and again.
237.2 -> So, I will separate this logic and
put it into function.
242.269 -> That will be easier for me.
243.733 -> Now, I will do one thing.
After that we will create function.
246.9 -> Suppose my requirement is to print "Done"
after writing "1+(a+b)/2"
255.432 -> As soon as I write this, I want to
print "done".
258.774 -> So, I have to write "console.log done"
262.733 -> Now, my requirement is to write
"Done" after every computation.
269.68 -> So, I have to write it in 3 places.
271.546 -> If my code is in use 30 times,
then I have to write it in 30 times.
275.837 -> But, with the help of function,
we can complete it by doing once.
279.051 -> And I will tell you how to do that.
280.494 -> So, we will create a simple function here.
283.467 -> And the code to create a function
will be like this.
288.534 -> I am making a function by the name of
"OnePlusAvg"
291.711 -> and I will take two numbers as "a and b".
294.353 -> And I will do return with "1+(a+b)/2"
299.7 -> To clarify your confusion,
I will make it as "x and y".
307.042 -> Now it is a function.
309.117 -> I want to keep this function at the top.
311.617 -> So, this function will take the input,
and will give the output.
314.316 -> Now, let's go the notes and see
how function works.
318.111 -> A JavaScript function is a block of code
designed to perform a particular task.
322.567 -> So, a particular block of code that
perform a particular task.
325.22 -> Syntax of a function looks something like this.
327.118 -> You write the name of the function here,
write the code after that.
330.323 -> Now, whenever you execute this function,
this will run.
334.224 -> You need to invoke this function
and tell explicitly that
337.835 -> you want to utilize this function.
Then only it will run, otherwise not.
342.564 -> You can make a Binod Function.
344.317 -> I mean to say, you can write any name
and create a function.
351.81 -> Parameter 1, parameter 2.
354.064 -> These are the parameters of the function,
and you can use the parameters.
357.294 -> Parameters will work as local variables
in the function body.
361.733 -> You can invoke the function like this way
and the code of the function will run.
365.233 -> In this case, I have to run "OnePlusAvg"
somewhat like this way.
371.645 -> Alright!
372.462 -> If I do that, onePlusAvg will print.
Let me show you.
376.542 -> 2.5, 3.5, and 3. Output remained the same.
379.248 -> Why so?
380.717 -> Because I returned the same logic here.
384.69 -> Now, here I will print "onePlusAvg b and c".
390.503 -> Here I will print "b and c"
394.527 -> And here I will print "a and c". Alright!
Like this way!
399.982 -> If I run this, output will remain the same
as 2.5, 3.5, and 3.
405.22 -> Suppose I need to print "done"
after I invoke this.
408.533 -> So, what I will do is that
410.1 -> I will write "console.log" and "Done".
414.431 -> Now see what happens!
417.379 -> I am going to run this.
onePlusAvg of a and b is this. Then Done.
422 -> Then this will print. (look at the cursor on the screen)
then done!
423.609 -> Then this will print. (look at the cursor on the screen)
424.649 -> Therefore, I must make all the
changes I want in just one place.
427.415 -> And that will be reflected everywhere.
429.455 -> Suppose, anybody asked you to round off
and print onePlusAvg.
435.079 -> If I write the logic of onePlusAvg 50 times,
437.647 -> Then I have to round off 50 times.
440.389 -> So, will I change it 50 times in my program?
No!
443.52 -> If I have created function, then I just
have to change it in the function
446.791 -> and the change will reflect everywhere.
448.632 -> Do the changes at one point,
450.091 -> and the function will give the convenient
to reflect the changes
452.884 -> Suppose, anybody asked you
to round it off.
457.139 -> So, I can use the logic to round off
and the work will be done.
460.641 -> The rounding off logic in JavaScript
looks something like this.
463.903 -> You can do "Math.round".
Alright!
466.982 -> And if I run it, it will print 3, 4, and 3.
Let me show you!
471.847 -> 3, 4, and 3 is printed.
473.74 -> So you can do the round off in
JavaScript this way.
475.737 -> But we will learn about Math.
477.4 -> Don't worry about all these things right now.
479.127 -> I just wanted to explain you the point
of creating function!
484.548 -> So, let's come this one again!
486.57 -> As we did "binod func 7, 8",
487.9 -> 7 and 8 will be copied here as
local variables.
491.96 -> And both are used under this function.
(look at the cursor on the screen)
493.529 -> And in this case, as I put "a and b",
the value of 1 and 2 will come here.
498.345 -> The numbers 1 and 2 will be copied in
lieu of the letters "x" and "y," respectively.
502.138 -> The computation here will be as "1 and 2"
in the place of "x and y"
507.762 -> And as soon as it returns the value,
511.058 -> the value will come in this place.
(look at the cursor on the screen)
513.423 -> That is why it is printing 2.5, 3.5, and 3 here.
516.851 -> For "b and c", it will be "2 and 3" here.
520.378 -> So, the value of "x" will be "2",
and the value of "y" will be "3".
522.95 -> then here it will be "1+5/2"
525.505 -> So, here 3.5 will return.
528.278 -> And finally, 3.5 will be kept here.
531.7 -> Sane goes for "a and c"
533.659 -> 1 and 3 will be copied in
the place of "x" and "y," respectively.
537.026 -> Then it will come as "1+4/2",
means "1+2".
541.772 -> And here it will replace by 3.
543.558 -> And that is how function works.
545.955 -> Now see, function invocation is a way
to use the code inside the function.
549.816 -> If I don't invoke the function
or I don't use the function,
552.505 -> then function will be left there simply
in the code.
554.864 -> It will not print anything.
556.928 -> Nothing will be seen.
558.88 -> And see, nothing is printed here,
neither in the code.
561.816 -> If you make the function like this,
then it will not run.
564.282 -> You have to invoke that by using this syntax.
567.62 -> Is that important to have parameters
in your function?
570.919 -> Well, No!
571.997 -> You can make a very simple function
that just prints "hello".
576.769 -> And I will give you the demonstration
right away.
579.523 -> First I will show you something here.
581.963 -> Even this will be a correct syntax if you
make the function this way.
585.089 -> Now observe how the function is
made up of sum.
587.651 -> Let me create a function and using a
different method and show you all.
591.598 -> So, I will write "const sum is equal to".
593.1 -> This is called "Arrow Function".
And we will learn about it next.
596 -> So, I did it this way.
597.751 -> Then I will do "return a +b".
600.839 -> Or else "p+q"
603.601 -> I could have used "a+b" also.
No problem!
606.62 -> I am doing this just for your convenient.
609.215 -> Now, if I write "console.log sum of 9 and 7".
616.132 -> So, it will print 16, of course.
619.637 -> You can see as it is printed 16 here.
621.52 -> This is also another method to create
a function.
623.698 -> Can functions be created without argument?
626.091 -> Well, Yes!
627.385 -> Suppose, here I create "function hello"
or "const hello is equal to function"
631.612 -> I will prefer this method
633.447 -> as it is the way to create function
in modern JavaScript.
639.044 -> I wrote hello is equal to this,
640.716 -> And here I will write "console.log hey how are you
644.945 -> I am to fine yaar".
647.231 -> Suppose I wrote this.
651.07 -> Run it.
653.316 -> You must be thinking that why "hey how
are you I am to fine yaar" is not printed!
657.419 -> Because, I didn't invoke this function.
660.983 -> So I have to call "hello" explicitly this way.
664.246 -> This is syntax, to call a function, which
doesn't return any value.
669.792 -> If it returned the value,
I would have given the parameters.
673.126 -> The function "hello" neither returns
any value nor takes any value.
677.623 -> So, this kind of function is also be made.
679.862 -> A function like this also can be created
which takes a value and returns nothing.
683.521 -> A function like this also can be created
which doesn't take a value but still returns.
687.355 -> So, this way you can return the value
through the 'return' keyword.
691.878 -> And if it is not returning anything,
then I can write "hello"
694.758 -> If it returns like this way like
"return hi"
703.402 -> And suppose, I write "let v is equal to hello"
706.942 -> Then "hi" would have come inside "v".
708.959 -> So, if I do "console.log v",
it will print "hi".
712.221 -> Let me show you.
714.793 -> So, You can see here it is printed "hi" .
716.344 -> Things that return, come inside this
and get replaced!
719.907 -> So, the value of "v" will be "hi"
722.915 -> So I hope you guys have understood.
724.473 -> We have created an "arrow function"
726.232 -> We have created an old function JavaScript.
729.136 -> This also works in today's JavaScript.
730.531 -> But we will prefer this type of syntax
in upcoming videos.
733.697 -> Also I prefer this type of syntax
in our production applications.
737.338 -> So,I want you too prefer
this type of syntax .
741.153 -> This is called "Arrow Functions.
Alright!
743.967 -> And I have written about it
in the notes.
747.721 -> So, here I have done sum of 1 and 3
and shown it to you all as it prints 4.
752.061 -> I have written this syntax here
and in Replit as well.
755.117 -> It will be easier for you all.
757.067 -> So I hope that you all have
understood function well.
759.298 -> Let me summarize once again.
760.711 -> Function is used to separate the logic,
763.797 -> If want to do any work where you need to
do the changes again and again.
767.533 -> single code is used 50 times in the program,
770.099 -> leave 50, even a code is used 6 times,
772.25 -> there you would like to change it at one
place instead of doing 6 times.
776.122 -> And can do it all with the help of function.
779.667 -> Alright!
781.3 -> And if you didn't have the access of the
JavaScript play list yet, then do it.
785.476 -> We will learn about "Arrow Function"
in upcoming videos.
787.849 -> We will learn what is Lexical this,
and details about the "Arrow function"
791.54 -> Now we will see in the basic
and will learn in upcoming videos.
794.414 -> We will then increase the level at which
I have provided the guarantee.
799.685 -> And if you still didn't have the access to
this play list,
801.583 -> then click here to bookmark and save it.
804.473 -> That's it for now.
Thank you so much for watching this video, Guys!
807.797 -> And I will see you next time!
Source: https://www.youtube.com/watch?v=a_gwOwkbhZ0