Variable Declaration Keywords

var

We use the var keyword to declare variables. The scope of a variable declared using this keyword is within the context wherever it was declared. For variables declared outside any function, this means they are globally available throughout the program. For variables declared within a function, this means they are only available within the function itself.

EXAMPLE
Run
Input
Output

Solution

Click Run above to execute the given code. It works in the following way:

  1. Variable is declared in the main function using the var keyword and initialized with the given value, .
  2. evaluates to true because is odd, so we enter the if block.
  3. Variable is declared a second time inside the if block (still using the var keyword) and initialized with a value of . We print the value of .
  4. We exit the if block and print the value of in main. This value is because the scope of the initial declaration of in main includes the if block.

let

We use the let keyword to declare variables that are limited in scope to the block, statement, or expression in which they are used. This is unlike the var keyword, which defines a variable globally or locally to an entire function regardless of block scope.

EXAMPLE
Run
Input
Output

Solution

Click Run above to execute the given code. It works in the following way:

  1. Variable is declared in the main function using the let keyword and initialized with the given value, .
  2. evaluates to true because is odd, so we enter the if block.
  3. Variable is declared a second time inside the if block (again using the let keyword) and initialized with a value of . We print the value of .
  4. We exit the if block and print the value of in main. Because we used the let keyword for both declarations and the scope of the second declaration of was limited to the if block, the value of in main is still .


It's important to note that you cannot redeclare a variable declared using the let keyword within the same scope as the original variable. An attempt to do this raises an Error, as demonstrated by the code below.

Run
Input
Output

const

We use the const keyword to create a read-only reference to a value, meaning the value referenced by this variable cannot be reassigned. Because the value referenced by a constant variable cannot be reassigned, JavaScript requires that constant variables always be initialized.

EXAMPLE

Click Run below to see what happens when you declare a constant variable without initializing it.

Run
Input
Output

 

Table Of Contents

JavaScript const Keyword
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score