Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the category
of reference types, you have learned about objects and arrays. Now,
let's take a look at functions. Functions are one of the fundamental
building blocks in JavaScript. A function is basically
a set of statements that performs a task or calculates
a value. Let show you a couple examples. So I'm going to
declare a function using the function keyword. Now we need to give it
a name, let's call that greet. After that we need to add
parenthesis, that's part of the syntax for declaring functions.
And then curly braces. Now what we have here,
inside the curly braces is what we refer to as the body of
this function. And this is where we add all the statements to define
some kind of logic in our application. For example,
The logic for this function should be to display a message
on the console. So here we can add console.log
Hello World.
Now note that here we have a statement, so we terminate it with a
semi colon, but when we are declaring a function, we don't need to add
semi colon at the end, because we are not declaring it like a
variable like this. Okay? This is a function
declaration, right? So, now we have a function
we can call this function like this. So we add the
name of the function, and parenthesis again, and then
semi colon to indicate that this is a statement. Save the
changes, now we have Hello World on the console. But that's pretty boring,
what would we do with this? Let me show you how to make this more interesting.
Our functions can have inputs, and these inputs can change
how the function behaves. So, let's say
instead of displaying Hello World we want to display the name of the person
here. Like Hello Jon. So we can add
a variable here in between parenthesis,
we refer to tis variable as a parameter. So,
this greet function has one parameter called name, and essentially
name is like a variable that is only meaningful
inside of this function. So inside of this function we can work with this name variable
but it will not be accessible outside of this function.
Now name is an input to this function. So instead of
displaying Hello World you can display Hello then add
a plus here to concatenate two strings.
So, we can add name after. Now,
when calling the greet function, we need to pass a value for the
name variable, or name parameter more accurately. So,
we can pass John here, now we refer to this
as an argument. So John is an argument
to the greet function and name is a
parameter of the greet function it's one of the things that a lot of programmers
know. They don't know the difference between a parameter and an argument.
So a parameter is what we have here at the time of declaration,
but the argument is the actual value of supply for that
parameter. Okay? Now, I save the changes,
so we have Hello John. Now we can reuse this function
but with a different input. So, we can
change this line here and change John to Mary. Save the changes
now we have two different messages on the console.
Now a function can have multiple parameters. So here we can have
separate parameters using a comma, so let's add another
parameter like lastName, now
we can change our console.log add a space here,
and then display the lastName.
Now, when calling this greet function, we should pass
another argument for the last name. Well let's see what happens if we don't
do this. So I'm going to save the changes, see what we got
Hello John undefined. Because as I told you before
that the fault value of variables in JavaScript is undefined.
So because we did not pass a value for the last name,
by default it's undefined. So I'm going to pass
another argument here, we separate them using comma
John Smith and we don't need this second call to the
greet function. Save the changes, now we have Hello
John Smith.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.