Day 9 of the 100 days of Miva coding challenge. Big progress has been made on my understanding of JavaScript.
Today, I learned about functions on JavaScript. Functions are very useful in JavaScript as they allow us to reuse code and also avoid mistakes due to code duplication. Thanks to W3schools, I was able to learn and practice functions and operations related to them. Let's dive in!
Creating Functions
Functions are created by using the function
keyword, followed by parenthesis containing the parameters required for executing the function. The return
keyword is used to state which operation is to be performed on the parameter(s) and what value to return to the user.
Using Functions outputs as variable values
Outputs of functions can be assigned to variable names after the function has been executed.
Functions have various uses and can be used to perform different types of operations ranging from arithmetic operations, logical operations and executing conditional statements. Here is an example of a function used to convert units:
Values obtained from these operations can be embedded in HTML programs that require values obtained from these functions.
Local Variables
In JavaScript, variables declared within a function are known as local variables and do not exist outside of the function. Hence, a new variable can be declared with the same name outside of the function which is normally not allowed.
Functions are very important in JavaScript as they make coding easier and cleaner. They allow code reusability and serve as mini libraries where outputs can be pulled for use in other parts of code.
That's it on JavaScript Functions. Hoping to explore more and share with you in the coming days! Source code can be seen here: https://github.com/Abdul-Samod/100daysofMiva/blob/ce61cd46cefb40058a83276484d45b1b27c27a38/javascript_prac2.html
Top comments (1)
w3School, building devs