Functions in JavaScript | Code Explained

Functions in JavaScript

Functions in JavaScript
What is a function in Programming?

A function is a block of code, that is organized and reusable.

why/when do we need a function?

We use a function when we have a block of code, that we will reuse multiple times. so to save time, we put all that code inside what we call a function, then we can run it, by simply, type in the function's name.

Create A Function in JavaScript.

To create a function in JavaScript, we use the keyword function, we can create it, using a function declaration or a function expression.

Function Declaration.

To create a function using a function declaration, we first type the function keyword, then the name of the function, then the parenthesis ( ), and then the curly brackets {}.

Example : 


function doSomething(){


        // code goes here.


}


To execute the code inside the function you must call the function first.
To call the function "doSomething" above, you have to run this line of code :




doSomething();




The name of the function + the parenthesis ().

Function Expression.

To create a function using a function expression, we first say, let then the variable name, (ex : let x ), then the equal sign "=", then the function keyword, then the parenthesis, then the curly brackets (and the code inside), and finally a semicolon ";".

Example : 


let x = function () {


            // code goes here.


};

Now to call the function above, we simply call the variable, like this :


                         
    x();




The function created using a function expression, is also called an anonymous function, as it has no name.

You can see this article in action, by watching my tutorial about functions in JavaScript below.

The Lesson : 
Practice : 



COMMENTS

Name

bootstrap,2,css,2,featured,3,JavaScript,19,
ltr
item
Code Explained: Functions in JavaScript
Functions in JavaScript
https://4.bp.blogspot.com/-M8TzY4T0eJA/W2txMP2ZL0I/AAAAAAAAAGE/Cn2TBDWuKMkYMpY3ZDcn5vu4dUndkc3YgCLcBGAs/s640/js%2Bfunctions.png
https://4.bp.blogspot.com/-M8TzY4T0eJA/W2txMP2ZL0I/AAAAAAAAAGE/Cn2TBDWuKMkYMpY3ZDcn5vu4dUndkc3YgCLcBGAs/s72-c/js%2Bfunctions.png
Code Explained
https://www.codeexplained.org/2018/08/functions-in-javascript.html
https://www.codeexplained.org/
https://www.codeexplained.org/
https://www.codeexplained.org/2018/08/functions-in-javascript.html
true
6721257432511051792
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy