JavaScript Functions

A JavaScript function is a block of "reusable" code designed to perform a particular task, it is executed when "something" invokes it (calls it).

A JavaScript function is defined with the function keyword, followed by the name and parentheses (). The parentheses may include parameter names separated by commas. The code to be executed, by the function, is placed inside curly brackets {}. Variables declared inside a function, become LOCAL to the function and can only be accessed from within the function.

Accessing a function without "()" will return the function object instead of the function result.

Function arguments are the values received by the function when it is invoked. Inside the function, the arguments (the parameters) behave as local variables.

Functions often compute a return value which is "returned" back to the "caller". When JavaScript reaches a return statement, the function will stop executing.

Functions can be used the same way as variables, where the returned value is assigned to the variable.

Another way of defining a function expression is an arrow function. An arrow function expression has a shorter syntax compared to function expressions and does not have its own this.

JavaScript has several built-in functions:

Built-in FunctionsDescription
eval()evaluates JavaScript code represented as a string
isNaN()determines whether a value is NaN or not
parseInt()parses a string argument and returns an integer
parseFloat()parses a string argument and returns a floating point number