Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Function Scope

JavaScript uses function scope, meaning each function creates its own scope.

Variables defined inside a function are not accessible from outside that function.

When declared within a function, variables using varlet, and const all have function scope and behave similarly.

function myFunction() {
  var carName = “Volvo”;   // Function Scope
}
function myFunction() {
  let carName = “Volvo”;   // Function Scope
}
function myFunction() {
  const carName = “Volvo”;   // Function Scope
}

Global JavaScript Variables

A variable declared outside of a function becomes Global.

Example

let carName = “Volvo”;
// code here can use carName

function myFunction() {
// code here can also use carName
}
A global variable has global scope, meaning it can be accessed by all scripts and functions on a web page.