Today #day31 of #100daysofcode, I have completed 4.24 and 4.25 lessons & Two code practice problems in JS course. @Sololearn
user defined functions
JavaScript Functions
- JavaScript function is a block of code written to do a task.
- code reuse, right it once and use code many times.
- you can pass different arguments and get different results.
- a Javascript function gets executed when it is called.
Defining a Function
- syntax:
function name() {//code to be executed}
- Functions are first class in JS. They are objects and can be passed around. This type of defining a function is called a Named Function. It's the same as var name = function () { }
- Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
Calling a Function
- when you call a function then only it will get executed.
- its imperative to include
;
at end after calling a function. - you can call a function many number of times once you define.
- syntax: myFunction.call()
function parameters
- functions can take parameters
- syntax:
functionName(param1, param2, param3) {// some code}
- Function parameters are the names written in the function's definition.
- like variables parameters are given names and separated by commas inside parenthesis.
Using Parameters
- you can use the parameters inside the function once you define them with variable name inside parenthesis.
- parameters value is called argument.
Note:
- A parameter name is defined inside a parenthesis while defining a function. whereas a parameter value or argument is put inside a parenthesis when calling a function.
Images
Conclusion
- JavaScript Functions
- Defining a Function
- Calling a Function
- function parameters
- Using Parameters