JavaScript is not a strongly typed language. What this means is that any variable can be any kind of variable type. So a variable that is a string, can be reassigned directly to a number.

In saying so, lets see how we can create some variables. There are generally 4 ways to assign variables in JavaScript. However, some are better than others.

The 4 main ways are:

KeywordUsage
varThis was how you used to assign variables in JavaScript, however this can cause scope issues (We will learn about scope later!).
constThis is a constant, so you can only assign it once, and it can’t be changed. Like creating a variable for “pi” (3.14). As this never changes, you can use a const as it is more memory effecient.
letThis is the standard way of creating a JavaScript variable. This can be assigned any value, can be reassigned at any time and is the one you will most likely be using.
Correct, nothing. You can create a variable without a keyword, however, this is mundo bad and should never be done! Most IDEs or Text Editors will let you know theres an error if you do this. However, it will still work.

No let see some examples:

// This is a standard variable that is assigned a number
let numberVariable = 0;

// This is a constant, meaning it cannot be reassigned any
// value after its initialization
const welcomeString = "Welcome to Bad Panda Code!";

// Below is a global scope array variable. 
var listOfNumbers = [1, 2, 3, 4];

That is how you assign variables. You will notice some things with the code above.

  1. //” – The double slash is how you can add a comment to the code. This is not read as code, this is used to add some notes to your code.
  2. ;” – This semicolon is optional, to end of a statement in JavaScript. However from languages like C# and Java where its required, not optional, I choose to add them anyway.
  3. [ ]” – Square brackets, these are how you create arrays! lists of numbers, strings or any other information. We will go into these in more detail later on.

Congratulations! You have leveled up in programming!


Next – Coming Soon

Previous – Javascript 2 Variables – Types

Leave a Reply

Your email address will not be published. Required fields are marked *