JavaScript: Step 4- Functions and Scopes

Functions in JavaScript are blocks of code that can be called multiple times and can optionally take in parameters and return values. Scopes refer to the accessibility of variables within a function or block of code.

  1. Function Declaration vs Function Expression:

There are two ways to define functions in JavaScript: function declaration and function expression.

Function Declaration:

function functionName(param1, param2, ...) {
  // code to execute
  return value;
}

Function Expression:

const functionName = function(param1, param2, ...) {
  // code to execute
  return value;
}

Function declarations are hoisted to the top of the code, meaning they can be called before they are defined. Function expressions must be defined before they are called.

  1. Function Parameters and Arguments:

Function parameters are the variables declared in the function definition. Function arguments are the values passed to the function when it is called.

Example program:

function greet(name) {
  console.log("Hello, " + name + "!");
}

greet("John"); // Output: "Hello, John!"

In the above example, name is a parameter and “John” is an argument.

  1. Function Return Values:

Functions can optionally return a value.

Example program:

function add(a, b) {
  return a + b;
}

let result = add(2, 3);
console.log(result); // Output: 5

In the above example, the add function returns the sum of a and b. The returned value is then stored in the result variable and printed to the console.

  1. Scopes:

JavaScript has two types of scopes: global scope and local scope. Variables declared outside a function have global scope, meaning they can be accessed from anywhere in the code. Variables declared inside a function have local scope, meaning they can only be accessed within that function.

Example program:

let globalVar = "Hello, World!"; // Global variable

function printLocal() {
  let localVar = "Hello, Local!"; // Local variable
  console.log(globalVar);
  console.log(localVar);
}

printLocal(); // Output: "Hello, World!" "Hello, Local!"
console.log(globalVar);
console.log(localVar); // Throws ReferenceError: localVar is not defined

In the above example, globalVar is declared outside the printLocal function and has global scope. localVar is declared inside the printLocal function and has local scope. The globalVar variable can be accessed from within the function and also outside the function, but the localVar variable can only be accessed within the function.

Understanding functions and scopes is crucial for writing efficient and reusable code in JavaScript. By breaking code down into functions and properly scoping variables, you can write more maintainable code that is easier to debug and update.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.