6.Functios in javascript Built-In,User Defined- Connecting HTML and Javascript

Опубликовано: 18 Октябрь 2024
на канале: Learning With Faraz
281
11

You Will learn
1.Functions in javascript
2.Built in functions
3.User-defined functions
4.Connecting HTML and Javascript


A function is a self-contained collection of statements that run as a single unit: essen‐
tially, you can think of it as a subprogram. Functions are central to JavaScript’s power
and expressiveness, and this chapter introduces you to their basic usage and
mechanics.
Every function has a body; this is the collection of statements that compose the
function:
function sayHello() {
// this is the body; it started with an opening curly brace...
console.log("Hello world!");
console.log("¡Hola mundo!");
console.log("Hallo wereld!");
console.log("Привет мир!");
// ...and ends with a closing curly brace
}
This is an example of a function declaration: we’re declaring a function called say
Hello. Simply declaring a function does not execute the body: if you try this example,
you will not see our multilingual “Hello, World” messages printed to the console. To
call a function (also called running, executing, invoking, or dispatching), you use the
name of the function followed by parentheses:
sayHello(); // "Hello, World!" printed to the
// console in different languages
103The terms call, invoke, and execute (as well as run) are interchange‐
able, and I will use each in this book to get you comfortable with
them all. In certain contexts and languages, there may be subtle dif‐
ferences between these terms, but in general use, they are equiva‐
lent.
Return Values
Calling a function is an expression, and as we know, expressions resolve to a value. So
what value does a function call resolve to? This is where return values come in. In the
body of a function, the return keyword will immediately terminate the function and
return the specified value, which is what the function call will resolve to. Let’s modify
our example; instead of writing to the console, we’ll return a greeting:
function getGreeting() {
return "Hello world!";
}
Now when we call that function, it will resolve to the return value:
getGreeting(); // "Hello, World!"
If you don’t explicitly call return, the return value will be undefined. A function can
return any type of value; as a reader’s exercise, try creating a function, getGreetings,
to return an array containing “Hello, World” in different languag



#FunctionInJavascript #JavascriptANDhtml #javascriptCourse2019