Javascript Basics

 Javascript Basics

 

Variables

We use a variable to store data temporarily in a computer’s memory temporarily. A variable is like a box (a container). What we put in the box is the value we assign to a variable that is the data and the label that we put on the box is the name of the variable.
Example 1: let bigbox = ‘manynewshoes’
Example 2:
let name;
console.log(name); //Will display in console undefined
Example 3:
let name = ‘Chaz’;
console.log(name); //Will display in console Chaz
Rules on variables.
  • Cannot be a reserved keyword.
  • Should be meaningful
  • Cannot start with a number
  • Cannot contain a space or hyphen
  • Are case-sensitive
let (use if you need to reassign variable)
const (use if the variable remains the same throughout.
What can we assign to variables? Primitives/Value Types and Reference Types
Primitives/Value Types
  • String (example ‘Chaz’)
  • Number (example: 30)
  • Boolean (true or false) Note: true and false are reserved keywords.
  • undefined (When a variable is not defined)
  • null (used to clear the value of a variable)
Examples:
let name = ‘Chaz’ // String literal
let age = 42 // Number Literal
let isApproved = false; 
let lastName = null;
Reference Types
  • Object
  • Function
  • Array
Javascript is a dynamic language. The variable can change at runtime.
We can use typeof (operator) to check the type of variable.
In Javascript we only have number. We don’t have floating point numbers and integers like other programming languages.