Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active February 21, 2024 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisterBrash/eb3d0f71e060f8063e835c2e07d34605 to your computer and use it in GitHub Desktop.
Save MisterBrash/eb3d0f71e060f8063e835c2e07d34605 to your computer and use it in GitHub Desktop.
ICS3U Lesson 1.2 - Variables

1.2 - Variables

ICS3U

The Lesson

(or jump ahead to your task)

Giving output is fairly easy with console.log( ). But what if we wanted to hold on to a value to be used later or for calculations? Perhaps we want to hold a lot of values like a quadratic equation, the locations of items in a 3D space, a list of colours, phone numbers, or financials.

Don't feel like reading? Watch this video.

Variables hold numbers or text directly in the memory in the computer. We reserve a block of space by asking for one, just like in math class:

Let w represent the number of watermelons
Let apple represent the number of apples
w = 2
apples = 7
Let shopping_list = w + apples

๐Ÿค” In the example above, what is the value of shopping_list?

Variables in JavaScript are very similar:

let w = 2;
let apples = 7;
let myList = w + apples;  // sets the variable 'myList' to 9
console.log(myList);      // output the value of 'myList'

Note - you might see the keyword var being used online instead of let. You should always use let when creating a variable.

After being declared, the variable can be used as much as you want. Think of it like a box, a bucket, or envelope.

๐Ÿ—’๏ธ Quick Recap:

  • To declare variables, we will use the let statement (similar to math class).
  • We use the 'assignment' operator (a single =) to assign or change the value.
  • It is not like algebra where left = right
  • Variables can be reused, manipulated, and destroyed.

For example, if we want to change the value of w to 4:

let w = 2;
w = 4;

โ˜๏ธ We did not use the keyword let both times, only when declaring the variable.

๐Ÿ“ Note: You can declare a variable without giving it a value.

๐Ÿ’ป Try it:

  • Create a new code file (call it 1.2 - Variables.js or something similar)
  • Create a variable called age and make it equal to your age
  • Output your age to the console
  • Run your code to see what happens

Variables can be different types of data:

let myNumber = -45;
let myString = "Some text, in quotes";
let myBoolean = true;   // or false

Text or Words (called "Strings")

Strings can be combined (concatenated) using the + operator

let myString = "This is text";
myString = "This " + "and " + "that";    //  the value is now 'This and that'

Strings cannot be subtracted!

myString = "This and that" - "that";   //  not possible

Booleans (true or false)

Anything with value is considered 'true'

let sample = true; // obviously true
let sample1 = 5;   // considered true
let sample2 = 0;   // considered false
let sample3;       // considered false

Need more help on variables? (Tutorial on variables) (w3schools reference)

๐Ÿ‘‰ Tip:

Code IDEs have numbers on each line to easily talk-about and find specific areas. If there is an error, the interpreter will give you the line number it thinks is the problem. For example, this error says the problem is on line 12:

/home/example/index.js:12
SyntaxError: Unexpected token ')'

Your Task

If you haven't already, create a new code file 1.2 - Variables.js
Part 1
  1. Create a variable length and set it equal to 10
  2. Create a variable width and set it equal to 5
  3. Calculate the area of a rectangle using length and width - store the answer in a new variable area
    (Note: to multiply, use the 'asterisk' *) Example:
let x = 3;
let multiplied_value = x * 4;   // This would be 12
  1. Output the value of the area variable to the console.
  2. Output the area again, but this time like this: Area: 50 {use the area variable for the number}
Part 2
  1. Create a variable called first_name and set the value to "Santa".
  2. Create a variable called last_name and set the value to "Claws".
  3. On the next code line, set the value of last_name to "Claus".
  4. Using the variables you made in 1 and 2, print "I still believe in Santa Claus." to the console.

"What do I do when I'm done?"

No need to humble brag. Find something to learn.
For example: learn how to start a C++ program and recreate the above task in C++.



#1.2 - Some More Examples

let a_sentence = "I'm a string!";
let some_number = 1024;
let watermellons = 72;
let x;        // x starts as "undefined", it has no value
let y = -6;   // y starts with the value -6
x = y + 10;   // Now 'x' has a value  (4)
y = x - 1;    // Now 'y' has a different value (3)
x = x + 1;    // Increment x by 1
x++;          // A shortcut for incrementing

/* Anything in "quotes" is text (called a String)
 * We can combine (concatenate) strings */
let first = "Mr.";
let last = "Brash";
let myName = first + " " + last;      
console.log(myName);   // Output: "Mr. Brash"

// We CANNOT subtract strings
myName = myName - "B";

Quick Note:

If you would like to output more than one thing to the console, you can use a comma-separated list. A space will be placed between each item.
For Example:

let x = 1;
console.log(6,"minus",x,"is",5);

// Output:  "6 minus 1 is 5"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment