Declarations

There are three kinds of declarations in JS.

  1. var declares a variable.

  2. let declares a variable that is block-scoped (more on this later).

  3. const declares a block-scoped variable, that is a constant.

Variables

A variable is a container of memory that holds a value. Declaring a variable is as simple as writing var myVariable;. A variable can be declared in this way, but does not have a value until it is initialized by assigning it one: myVariable = 0;. In JavaScript we can declare a variable and initialize it at the same time: var myVariable = 0;. It can contain virtually anything, but it has certain naming conventions that must be followed:

  1. A variable MUST begin with a letter a-z, an underscore _ , or a dollar sign $ .

  2. Numbers 0-9 may follow any of these characters h3ll0.

  3. JavaScript is a case-sensitive language: HELLO and HeLLo are two different variables.

Declaring Variables

There are three ways to declare a variable in JavaScript:

  1. The var keyword: var newVariable;

  2. Assign a variable a value: newVariable = 12

  3. The let or const keywords: let newVariable = 12; const newVariable = 12

File Location

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
                01-comments.js
                02-declarations.js <-- You are here

Practice

  1. In declarations.js, use the var keyword to create two variables and assign each a value.

  2. Print both variables to the console.

  3. Change the values in each variable, then print both to the console again.

Last updated