Table of Contents
Variables and Data types are the basic concepts of any programming language.
First of all, variables are like containers in which we can store values in order to use them over and over again in our code. By saving values, we don’t have to write them every time we need to use them.
Data types are classifications of data. Variables can hold various data types like – numbers, strings, booleans etc. Data types are important to work with variables.
Because it doesn’t make sense to, let’s say, add numbers with words or subtract numbers from words. If you try to add 21 with the word “programming” what result will it produce?
Now add the following to your HTML file using any code editor.
Like we said earlier, variable can hold a value that we can use as many times we want without writing the value again and again.
var name = "Imtiaz Rayhan";
This stores the value “Imtiaz Rayhan” to the name variable. If we want to use this value anywhere in our code, we just call the variable name.
Now, to check if it’s working, we’ll use console.log() method. Use it like this:
var name = "Imtiaz Rayhan";
Save the script.js file with this and open index.html file on your browser. You can’t see anything yet. Right click on the page and click on Inspect Element.
Now you can see Console, click on that and you’ll see your value printed there.
Let’s check a variable with a number.
var age = 21;
You should see the number 21 on your console.
var a; // Here a is undefined
var a = 10; // Here a is a Number
var a = "Imtiaz"; // Here a is a String
Now, let’s check out the data types that we can use with variables.
Primitive data types are single data types. That means primitives can hold only one value.
Another data type is Objects. Objects can hold multiple values.
For example, having a 10 means having 10.0 as the value.
In other programming languages, you may have different data types for integers or decimals.
var a = 10;
var b = 10.7;
var c = -10;
These are some examples of number data types. You don’t need any special syntax for numbers, like for strings you need to use quotes.
Therefore, if you use quotes with numbers, they will be considered as a string.
Furthermore, you can do regular math with number data types.
Strings are series of characters and any text is represented by a string.
A string is usually wrapped in quotes. You can use double quotes or single quotes, whichever you like. Also, you can use quotes inside a string but both end of the string must match.
var single = 'This variable is single quoted with a "double quote"';
var double = "This variable is double quoted with a 'single quote'";
Hence, if you use single quote to surround the string, you can use double quotes inside the string. And if you use double quotes to surround the string, you can use single quotes inside the string.
Boolean is a logical data type that can take one of the logical value of true or false.
To make it simple, boolean is either true or false. And this data type is mostly used to check if statements or conditions are true or false.
Let’s look at an example:
/* Check if one number is greater than the other*/
10 > 13; // This will return false.
var name = 'Imtiaz';
name === 'Imtiaz'; // This will return true.
Booleans are used in all programming languages when a value will change between true or false.
We declare a variable but we don’t assign any value to it. It will return undefined.
var name; // This returns undefined
We can also empty a variable using undefined as the value.
person = undefined; // The value and type, both are undefined.
If we declare a variable without using var, it’ll undefined too.
A NULL simply means nothing.
It’s a value that represents nothing which means, currently that variable does not have any value but it will have later on.
var nothing = null;
In the above example, null is assigned to a variable nothing. It means we have defined a variable but have not assigned any value yet, so value is absent.
Over to You
Those are a bit complicated than these. I’ll talk about them in details in our future posts.
Let me know through comments if you have any questions.