JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Variables
  • Declaring Variables
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 1-Grammar and Types

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.

PreviousCommentsNextScope

Last updated 7 years ago