JavaScript Function is a fundamental building block. Functions let us wrap bits of code up into Resuable packages.
Declare a function first:
function doSomething() {
console.log("");
}
We define a block of code and give it a name so that makes the function. Then we have to run it later.
Then call it:
doSomething();
doSomething();
doSomething();
For example:
We write a function named sayHi, and we want to print "Hello" when we run the code. When I call sayHi() and enter, the code runs and gives us "Hello".
However, if I only call sayHi without (), the result would be different. It will only gives us the function back.
How to write a function to repeat a song?
We can write function named singSong, then write each sentence of "Soft Kitty Warm Kitty" song in the console.log. At the end, we call the singSong(), the code will run. Every time we write singSong(), it will repeat the same song every single time.
In the real practice, if we write 50 lines of code , we can use the function and only call the name, it will run every time we call, so it saves a lot time.
Comments
Post a Comment