DEV Community

Sathish K
Sathish K

Posted on

Day 4: Return and Scope Java Script

what is return of function ?

The return statement is end function execution and specifies a value which return the function caller.

function myFunction() {
  return Math.PI;
} 


Enter fullscreen mode Exit fullscreen mode

*what is block level scope ? *

The block scope of a variable means that the variables can be accessible within the block which is between the curly braces.
This includes if statements, for and while loops, switch statements, and even standalone blocks.

{
  let x = 2;
}
// x can NOT be used here 

Enter fullscreen mode Exit fullscreen mode

what is global level scope ?

In JavaScript, global level scope refers to the outermost or top-level scope within a program. Variables, functions, and objects declared in the global scope are accessible from anywhere within the JavaScript code, including within functions and blocks.

// Global variable
let globalMessage = "Hello from the global scope!";

function greet() {
  // Accessing the global variable
  console.log(globalMessage);
}

greet(); // Output: Hello from the global scope!

// Another global variable
const PI = 3.14159;
console.log(PI); // Output: 3.14159

Enter fullscreen mode Exit fullscreen mode

what is function level scope ?

JavaScript has function scope: Each function creates a new scope.
Variables defined inside a function are not accessible (visible) from outside the function.Variables declared with var, let and const are quite similar when declared inside a function.

function myFunction() {
  var carName = "Volvo";   // Function Scope
}

Enter fullscreen mode Exit fullscreen mode

what is alert () function ?

The alert() function in JavaScript is a built-in method of the Window object, used to display a modal alert box with a specified message to the user. This alert box typically contains the message and an "OK" button.

Top comments (0)