Functions in JavaScript allow users to code one block of code designed to perform a particular task, and then call that code as many times as needed. All functions have brackets to contain the function code, and functions can be called by typing the function name followed by () and a semicolon. Functions can hold any amount of code, but efficient functions should not be too long. Functions can contain if, while, and for statements.
Functions can be passed arguments known as parameters; parameters can be modified and are placeholders for different arguments. A function will take a parameter and either modify or use it for calculations. Depending on the type of function, sometimes the function will return an output, whether computing a value or returning a string, but it doesn’t necessarily have to. For example, if you call a function designed to modify an existing value that is passed an argument, it might not return anything, but if you output the value in the console, it will be different than it originally was. For functions that return a value, that value can be stored in a new variable. The return keyword should always be at the end of the function because the computer will not run code after it recognizes the return keyword.
Leave a Reply
You must be logged in to post a comment.