JS Variables

Variables

A variable is like a mailbox for computers. It is used to hold a value. That value can be a number (an integer or double) or a word (a string).

Prerequisite

This page assumes you have completed the Coding Intro lesson.

Intro

We can create a variable within C# by using the keyword var. For Python, we don’t need to add a keyword; simply assign a name and that name is now a variable. In this example, we assigned the name myVariable.

let myVariable = 5;

Variable names should be meaningful. You should know what a variable is holding based on the name we give it. This makes the code much easier to read and understand what’s happening inside. We can give the variable any name, but the name we create can’t contain spaces, can’t start with a number, and can’t contain a symbol.


Variable Types

Variables in JavaScript can be Boolean, number, or strings. The language dynamically selects the appropriate type.

// creates a bool
let number = true;

//creates a number
let fraction = 3.5;

//creates a string
let name = "John";

JavaScript is dynamic and allows to switch types. In the following example, the first line will create a variable named x, since this is the first time we assign a value to x. The variable type will be an integer. The second line will change the variable type to a string and assign “Hello” to it.

x = 10
x = "Hello"

Arrays

Arrays are a way to store a list of variables that are the same type into one variable name. Let’s create an array of string variables called flowers, and initialize it with values.

let flowers = ["Roses", "Violets", "Daisies", "Mums", "Orchids", "Lilies"];

We can access and assign variables to specific spots in an array. We do this by including a number in brackets after the name, as shown below.

await BrainPad.System.Print(flowers[1]);

You may wonder why ‘Violets’ appeared instead of ‘Roses’? This is because arrays start counting from zero. To print ‘Roses’, we would have to use flowers[0].

We can easily set a value to a specific spot in an array just like we would set any other variable. It just has to be the same variable type as the array we created.

flowers[3] = "Marigolds";

Now the flowers[3] slot has replaced ‘Mums’ with ‘Marigolds’.


What’s Next?

Learn about Loops and how they are used to repeat tasks.


BrainStorm

We know that anything between double-quotes is a string. (JavaScript allows single-quotes as well). With that in mind, what is the difference between 53 and “53”? Can you guess what this code will print? Try it!

By the way, the last line differs slightly as C# and Python handle adding to a string differently. When adding a string to a string (“5″+”5”), the result is a one string that combines both strings (“55”). But adding a numerical value to a string (“5” + 4) does not work in the same way between these languages. C# automatically converts the numerical value to a string, such as a 4 becomes “4”. But Python does not do this and will issue an error. We will need to tell it to change the value to a string by adding str before the (numerical value).

await BrainPad.System.Println(53);
await BrainPad.System.Println("53");
await BrainPad.System.Println(53 + 1);
await BrainPad.System.Println("53" + "1");
await BrainPad.System.Println("53" + 1);
Content Licensing
Newsletter

Twitter Feed
Hot off the press!