JavaScript Data Types & Variables By Nmesoma Ogbonna

Learn the types of data types as well as everything you should know about variables in JS as I explain them in the simplest terms.

·

4 min read

In this article I'm going to help you understand fully the different types of data types in JavaScript, What Variables are and how to declare and assign values to them, but to understand how all of these works, we need to know what JavaScript is. JavaScript often shortened to jS is a scripting language created with the sole purpose of making the web more dynamic. It adds functionality to web pages and I'd give an example, When you click a button on a webpage and want something to change, you're utilizing JavaScript. It is a highly versatile and flexible language and has become the most commonly used language of the web.

Data Types

A data type in JavaScript as the name implies refers to a kind of data, and can be defined by the values such data can hold and the operations that can be carried out. To understand the type of data you're dealing when writing codes, you should know the different data types available to you. JavaScript has 6 primitive data types but I'd explain 5 as (Symbol) is new to EcmaScript 6.

  • Boolean: This data type can hold only two values; true or false.

    E.g

     let isCooking = true; 
     let isWalking = false;

The above example the first variable isCooking is assigned the value true.

  • Undefined: This data type can only have value which is "undefined". This happens when a variable is declared but no value has been assigned to it yet. E.g
     let firstName;
     let lastName = Ogbonna;
     alert(firstName);//output:undefined
     alert(lastName); // output: Ogbonna

In the example we declared two variables but assigned a value to only one(lastName), hence when we alert both varaiables, the firstName is returned as undefined.

- Number: This data type is used to represent numbers(positive or negative). E.g

     let a = 40;
  • Null: This is a special data type that has only one value, which is a - null value. A null value means there is no value and it is not equivalent to an empty string " " or 0. E.g
     let b = null; 
     alert(b); // output: null
  • String: This data type is used to represent textual data. Strings are created using single or double quotes surrounding one or more characters. E.g
      let c = "Welcome to my Blog!"

What is a Variable?

Variables are containers used to store data, the value or data stored in variables can be set, updates and retrieved whenever they are needed. Variables are fundamental to most programming languages and are important because they are used to store value. Each variable consists of two components; the keyword(a special word that lets the JavaScript interpreter know that you're creating a variable) and the variable name. JavaScript recognizes three different methods of declaring a variable(keywords); var, let and const. They each behave differently and have slight differences, var is used in older versions of JavaScript ES5, while let and const are mostly used in newer versions (ES6). E.g

     let myVariableName;

Assigning Values to Variables

In the line above, we declared a variable(myVariableName) but didn't assign a value to it, when this variable is returned it would be undefined because there is no value assigned to it yet. Now let's learn how to add a value by assigning a value to the existing variable. E.g

     let myVariableName = "Soki";

Also note that the value stored initially in a variable can vary/reassigned. The syntax for re-assigning a variable is;

     let age = 40;
     age = 42;

Here we just re-assigned a new value(42) to the variable (age).

Concatenation

In JavaScript we use concatenation to combine a variable to another string, to do this we add a plus sign + between the strings or variables we want to connect. E.g

     let amount = 5;
     let item = "cooking pots";
     let decription = "We have " + amount + " different " + item + " in our house. ";

In the example above we were able to concatenate two variables and multiple strings to form a sentence "We have 5 cooking pots in our house."

Interpolation

This is another method of adding values to strings and this method employs the use of template literals that allows us to interpolate information. It uses back ticks (`) instead of normal single/double quotes and is used with a combination of a dollar sign($) followed by curly braces {}. Let's look at an example using the variables from the previous paragraph.

     let amount = 5;
     let item = "cooking pots";
     let description = `We have ${amount} different ${item} in our house.`;

This is a shorter way of writing the same sentence and it helps reduces errors.

Conclusion

We learnt the different data types in JavaScript, what are variables and how values are assigned to them. We also discussed the difference between concatenation and interpolation. I hope this was helpful.

Happy Coding!