The predefined Rules for Writing & Using JavaScript Functions

navya , Credit to  volkotech-solutions Nov 03
Predefined rules of JavaScript Banner Image

Get sufficient info to learn the predefined rules and best practices when working with functions in JavaScript along with creating reusable functions.

Contents of JavaScript

  1. INTRODUCTION OF JAVASCRIPT
  2. JAVASCRIPT FUNCTIONS
  3. DECLARING FUNCTIONS
  4. CALLING A FUNCTION
  5. FUNCTION PARAMETERS
  6. FUNCTION RETURN
  7. FUNCTION OF EXPRESSIONS

Introduction of JavaScript:

  1. JavaScript is a high-level programming language that makes users interact with applications.
  2. It depends on the basic HTML and CSS and formats the context.
  3. The most dynamic part of the content to be formatted by the javascript, it works as a backend of the application.
  4. So here we will be discussing the usage and JavaScript Functions
  5. So let's get into this.

JavaScript Functions:

In JavaScript, we deal with different types of functions that can be reusable and non-reusable.

We have 3 different types of functions:

  1. Arrow functions
  2. Named functions
  3. Functions expressions

Declaring a function:

  1. Here in the below image, we have declared a function named logger.
  2. Inside the function, we have the body content that tells what must be executed in the output.
  3. Functions can be declared using the function keyword.
  4. It is always better to start descriptive writing for the function name.
  5. The overall body of the function will be declared within flower brackets
  6. In this image given below, it is simply declared with console.log of the context to be executed from the body
  7. You might be getting doubt that this is the only procedure to get the output? The answer is no! We have many procedures to get the content that will be explained in detailed methods further
function logger(){
console.log("my name is jonas");
}
//calling a function
logger();
logger();
logger();

Calling a function:

In the above program, we have declared a function named logger(). To use that function, we need to call it.

Here’s how you can call the loggers() function

  1. These are the defined procedures that function followed by semicolon logger();
  2. In the image below you can find the function call as logger();
  3. And there is another rule that you can call the function as many times as you want to.
  4. As long as you call the function it checks whether the function name is declared are not, If it is declared and the body is present it will execute the function
  5. So here in the below example, we have called the functions three times so the output will be printed three times.
  6. The body present in the logger function is My name is Jonas and this will be printed three times in the output.

Function Parameters

  1. The function parameters play a major role in the function call
  2. When you call a function you can pass the parameters (a value) through the function call
  3. In the below example code we have a function named fruit processor
  4. In the function, we have declared a variable called juice which has context juice with ${} apples and ${} oranges
  5. So here in the below example line no 29 we are passing two parameters for apples and oranges and calling the function
  6. When a function call happens these values pass into the declared functions which have arguments predefined and execute the required content
  7. That is how you pass the parameters while calling the function, Later you will get to know how do we store the function calls and return the variable in further lectures
function fruitProcessor(apples, oranges){
const juice = "juice with" $(apples) apples and $(oranges) "oranges.";
return juice;
}
const appleJuice = fruitProcessor(5,0);
console.log(appleJuice);
const appleOrangeJuice = fruitProcessor(2,4);
console.log(appleOrangeJuice);
const num = Number('23');

Function Return:

  1. This is another method that says how to print the variable
  2. The return statement is also a predefined rule of JavaScript functions which returns the value to be executed.
  3. It also states that the function declarations have come to an end
  4. If there is no value declared in the variable it prints an undefined value in the output
  5. In the code above we have specified return juice (variable)
  6. This returns the variable juice which has the content present in it when the function is called
  7. The return statement returns all types of data type

Function Expressions:

  1. In this lecture, you will get to know about function expressions and what is the difference between functions expressions and functions declaration
  2. Here in the image below, I have declared two functions in that one is functions followed by the function name and another one is a direct function that is stored in the variable name
  3. The storing of function in a variable and calling that function variable is called as functions expressions
  4. Here is the predefined syntax that one has to follow while declaring functions expressions
  5. Const variable = function name() {}
  6. Now you can call that function variable by passing the parameters
  7. So you know you have got to know about functions expressions
function calcAge1(birthYear){
return 2037-birthYear;
}
const age1 = calcAge1(1991);
console.log(age1);

const calcAge2 = function (birthYear){
return 2037-birthYear;
}
const age2 = calcAge1(1991);
console.log(age1, age2);

Conclusion

We should be able to create reusable functions in order to be more efficient in programming. We must complete all of the steps necessary to create a function. Now is the time to write some JavaScript functions and have some fun.

FAQ

1. Are JavaScript functions case-sensitive?

Yes, JavaScript is a case-sensitive language. The functions names in JavaScript typically must be in lowercase.

2. What types of functions will be supported by javascript?

It supports both named and anonymous functions while processing.

3. What is an anonymous function in javascript?

Usually, they are declared similarly as normal functions but without the name of the function.

 

Comments

Authors

Read Next