Function
They do certain work and can be called many times.
function nameOfFunction (parameters) {
main body
}
function greet () {
console.log("Good Morning");
}
greet();
Local Variables
variables declared inside the function which can only be visible inside the function
Global Variables
variables declared outside the function. All the function can use this variable
Parameters
The values which we have to later pass into a function is called parameter.
function findArea (length, breadth) {
console.log(length * breadth);
}
findArea(2,3);
Argument
When a value is passed as a function parameter, it is called argument
How to give default values to parameters
function findArea (length = 5, breadth = 7) {
console.log(length * breadth);
}
findArea();
Returning a value - VIP
function findArea (length, breadth) {
return length * breadth;
}
console.log(findArea(3,4));
If we only return; then the function exit immediately and return undefined and all the code below that wont execute
Never Add a new line between return and value because it will return undefined.
While naming a function, since functions are actions their name is usually verb. But for arrow functions which return variables must be noun.
While making function we must follow rule of one function - one action. If there are two actions, we need two functions. It is better for industry practice. Thus function should be short and exactly do one thing, if it is big may be we should split into few smaller functions. This simple practice can make you a good developer later.
Arrow Functions - VIP - most important for react
It is cleaner and better than function expression.
const greet = () => {
return "Good Morning";
}
console.log(greet());
If we have only one parameter we can skip the round bracket
If we have single line main body, we can skip the curly braces as well as return.
HomeWork -
- Create an arrow function that returns "Good Morning".
- Create an arrow function that returns your name.
- Create an arrow function that adds two numbers.
- Create an arrow function that multiplies two numbers.
- Create an arrow function that calculates the square of a number.
- Create an arrow function that returns the first character of a string.
- Create an arrow function that returns the last character of a string.
- Create an arrow function that converts minutes to seconds.
- Create an arrow function that converts Celsius to Fahrenheit.
- Create an arrow function that calculates the area of a rectangle (length * breadth).
- Create an arrow function that calculates the volume of a cuboid (l * b * h).
- Create an arrow function that returns a greeting with the user's name.
- Create an arrow function that checks if a number is greater than 100.
- Create an arrow function that checks if a number is even.
- Create an arrow function that returns the length of a string.
- Create an arrow function that joins two words with a space.
- Create an arrow function that adds three numbers.
- Create an arrow function that returns the type of the input.
- Create an arrow function that returns the full name from first and last name.
- Create an arrow function that adds 10 to a number.
- Create an arrow function that doubles the number.
- Create an arrow function with default parameter of name = "Guest", and returns "Welcome, Guest".
- Create an arrow function that subtracts two numbers.
- Create an arrow function that returns the perimeter of a rectangle.
- Create an arrow function that calculates the simple interest (P * T * R / 100).
- Create an arrow function that returns the age next year.
- Create an arrow function that returns the reverse of a string.
- Create an arrow function that returns the third character of a string.
- Create an arrow function that checks if a number is less than 50.
- Create an arrow function that checks if two numbers are equal.
- Create an arrow function that converts hours to minutes.
- Create an arrow function that returns the sum of digits of a 2-digit number.
- Create an arrow function that returns a fixed message: “Welcome to JavaScript!”
- Create an arrow function that calculates speed given distance and time.
- Create an arrow function that returns whether a string contains the letter "a".
- Create an arrow function that returns the cube of a number.
- Create an arrow function that returns the remainder when a number is divided by 5.
- Create an arrow function that adds a number to itself.
- Create an arrow function that converts years to days.
- Create an arrow function that returns the average of three numbers.