Fundamentals (Paid)
  • 🚀Course Overview
  • Course Logistics
    • 🏫Course Methodology
      • 🧩Course Components
      • 💬Community Channels
      • 🎲Course Projects
    • 💻Required Hardware and Software
      • ☝️Required Software 1
      • ✌️Required Software 2
      • 👍Recommended Setup
    • 🗓️Schedule
    • 💡Tips and Tricks
      • 📒Coding Strategies
      • 🛠️Tooling Pro Tips
    • 🎓Post-Course
      • 🎓LinkedIn Certificates
      • 🚂Bootcamp Admission Criteria
  • 1: Introduction
    • 1.1: What is Coding?
    • 1.2: Web Browsers
    • 1.3: Command Line
    • Additional Resources 1
  • 2: Basic Data Manipulation
    • 2: Operators and Expressions
      • 2.1: Arithmetic Operators | Mathematical Expressions
      • 2.2: Assignment Operators | Variables
    • 2.3: Our First Program
    • Additional Resources 2
  • 3: Structuring and Debugging Code
    • 3.1: Functions
    • 3.2: Errors
    • Additional Resources 3
  • 4: Conditional Logic
    • 4.1: Intro to Logic
    • 4.2: Pseudo-Code, Boolean Or
    • 4.3: Boolean AND, NOT
    • 4.4: Input Validation
    • Additional Resources 4
  • 5: Managing State and Input Validation
    • 5.1: Program Lifecycle and State
    • 5.2: Program State for Game Modes
    • Additional Resources 5
  • 6: Arrays and Iteration
    • 6.1: Arrays
    • 6.2: Loops
    • 6.3: Loops with Arrays
    • Additional Resources 6
  • 7: Version Control
    • 7.1: Git
    • Additional Resources 7
  • 8: GitHub
    • 8.1: Intro to GitHub
    • 8.2: GitHub Fork and Clone
    • 8.3: GitHub Pull Request
    • 8.4: GitHub Repo Browsing
    • 8.5: Deployment
    • Additional Resources 8
  • 9: JavaScript Objects
    • 9.1: JavaScript Objects
    • 9.2: Card Deck Generation with Loops
  • 10: Advanced
    • 10.1 HTML
    • 10.2: CSS
    • 10.3: The Document Object Model
    • 10.4: DOM Manipulation
    • 10.5: Advanced Debugging with Sources Tab
  • 11: POST COURSE EXERCISES
    • DOM
    • Further Readings
  • In-Class Exercises
    • Day 2: Basic File and Data Manipulation
    • Day 3: Functions
    • Day 4: If Statements, Boolean Or, Boolean And
    • Day 5: Program State
    • Day 6: Scissors Paper Stone Redux
    • Day 7: Loops
    • Day 8: Arrays and Loops
    • Day 9: Beat That Redux
    • Day 10: Moar Cards / Chat Bot
    • Day 11: Blackjack Redux, DOM
  • Projects
    • Project 1: Scissors Paper Stone
      • Project 1: Scissors Paper Stone (Part 1)
      • Project 1: Scissors Paper Stone (Part 2)
    • Project 2: Beat That!
    • Project 3: Blackjack
  • Past Projects
    • Drawing With Emojis
    • Guess the Word
Powered by GitBook
On this page
  • Learning Objectives
  • Introduction
  • What are Variables?
  • The Importance of Abstraction
  • Variable Naming Convention
  • Exercises
  1. 2: Basic Data Manipulation
  2. 2: Operators and Expressions

2.2: Assignment Operators | Variables

Previous2.1: Arithmetic Operators | Mathematical ExpressionsNext2.3: Our First Program

Last updated 8 months ago

Learning Objectives

By the end of this lesson, you should be able to:

  • Describe what a variable is and how to assign it a value or expression.

  • Use the var keyword to declare a new JavaScript variable.

  • Explain how to achieve accurate representation of your program with suitable variable names.

  • Use the camelCase naming convention for your JavaScript variable names.

Introduction

To do more than just basic math, we need to store, access and manipulate data in a computer's memory. We do this by utilizing variables in programming.

What are Variables?

Variables store and contain data. We name a variable and we associate a specific data value with it. We use variables to represent data that our program will process. First, we name a variable by using the keyword var. This tells the browser that the next word is the intended name for the variable.

Keywords are words that have a pre-defined meaning within a programming language, and are reserved for that use only. While all programming languages will have some way to declare a variable, different languages will do so using different keywords or syntax.

In order to assign a value to a variable, we use the assignment operator =. In programming, the equals sign has a slightly different meaning than in a mathematical equation, we are using = declaratively:

var pi = 3.14;
var radius = 4;

In the above statements we have declared the variables pi and radius, and assigned them the value 3.14 and 4 respectively. Using these 2 data values we can calculate the area of a circle.

pi * radius * radius;

We can also use variables to capture the result of that calculated value, and store it in memory.

var area = pi * radius * radius;

Note: You may have seen let and const syntax being used to declare variables in JavaScript. let and const are both relatively new ways to declare variables in JavaScript, and while let has a similar use case to var, they are different in ways which we will not delve into right now. For the purposes of Coding Fundamentals, it will suffice to use var, but you are free to explore and experiment.

The Importance of Abstraction

If we wanted the same result we could also write the following.1var x = 3.14 * 4 * 4;Copied!But this would not be as meaningful as our previous example.Our programs must not only calculate values correctly, they must also accurately represent the operations we are performing, in this case calculating the area of a circle. The names of our variables give meaning to the data our code contains.

Variable Naming Convention

Exercises

Using the Chrome Dev Tools Console, write code to represent each of the following. Use descriptive variable names that explain what you are calculating. You will need to search the web for some formulas. When you are done, paste the code into a message to yourself in the community channel.

  1. Calculate the circumference of a circle given its radius

  2. Convert Celsius to Fahrenheit

  3. Calculate the volume of a cube given a side length

  4. Calculate the gradient percentage of a road given rise and run

  5. Convert millilitres to pints

Drake is not known for his programming skills. Don't be like Drake.

In JavaScript convention, ordinary variables are named with . The name starts with a lowercase letter and every subsequent word starts with uppercase. For example, bananaCount or firstDiceRoll. This is convention, and will not affect code functionality. Different languages and communities have different conventions. Similar to driving conventions: in some countries, cars are driven on the left side, and on the right side in other countries.

lower camel case