Javascript in 15 Days : Variables

March 3rd, 2020

Welcome!

Before we begin, if you haven’t seen Day 1, click on the link below!

Javascript in 15 Days : Pseudocode

So, let’s begin. Today we will be talking about variables. What are variables you may ask? Well, let me explain. Think of variables as buckets. They exist to hold whatever is inside it. In the Javascript context, we assign data into it. Which is the same as throwing a piece of paper into the bucket.

Introduction

Variables in Javascript mainly consist of these data types. The 5 main data types are

  • string
  • number
  • boolean
  • null
  • undefined

So let me explain for each and every one of them. String. A string is basically a set of characters and has two basic steps. First, declaration. You start with the keyword var, let or const. For now, we will be using var as I will be explaining this in another class. For the second part, you encapsulate them with a single quote. But wait, it doesn’t necessarily have to be single-quotes! It can also be in double-quotes and also backticks. But, make sure that it starts and ends with the same symbol! Next up, numbers. Basically the same as strings, except they do not use the strokes. Javascript numbers are mostly integers. Boolean. They are either true or false. Let me show you in a bit. Null is basically a valueless variable. Undefined is any variable that has been declared but still holds no value, much like null.

Demonstation & Examples

Before I demonstrate, let me explain what I will be doing. Upon completion of my demonstration, we should be able to “declare” a variable and store values within it, regardless of the data type. And I will also show a bit on how to combine two of the variables and print it out on the console.

var firstName = 'John';
var lastName = 'Doe';
var myAge = 22;

var fullName = firstName + ' ' + lastName;
var alsoFullName = `${firstName} ${lastName}`;

console.log(fullName); // prints: John Doe
console.log(alsoFullName); // also prints: John Doe

var fatherName = 'Jack Doe';
var fatherAge = 43;

var ageDifference = fatherAge - myAge;

console.log(ageDifference); // prints: 21

console.log(
    `Hi, my name is ${fullName}. My father's name is ${fatherName}, and he is ${ageDifference} years older than me!`
);
// prints: Hi, my name is John Doe. My father's name is Jack Doe, and he is 21 years older than me!

In the above, we can see that I use the word var to declare the variable and also set value to it. We can also learn that there are two ways to truncate a string. By using the ‘+’ symbol, or by using the backticks and dollar signs, better known as template literals. We also know that we can perform mathematical operations by using the ‘+’ symbol too. For more info on mathematical operators in Javascript, click here.

Additional Resources

Now that we know a little bit about variables and datatypes, I would like to add a little bit, to enrich your knowledge and make this time worth your while. There are manipulation methods you can use to format your variables into something more unique. You can find all about them on the links below.

strings

numbers

Summary

I guess that’s it for today! I hope you liked it and I’ll see you tomorrow, we’ll be discussing conditionals! See you soon!!

Javascript in 15 Days : Conditionals