Friday , 17 November 2017
Home / Programming / JavaScript / Data Types – JavaScript Tutorial
Data Types
Data Types

Data Types – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

When working with variables, you have to think about the data types you want. While you don’t have to define the data types for any of your variables, it’s always good to know what data type your variable is.

What are data types?

In programming, all data you create can be categorized into what kind of data it is. Is it a string, integer, boolean, or an object? There are different kinds of data types. Here is what they are.

  • String –  a variable that can store a set of characters. Characters are basically the keys on your keyboard. Every letter, number, and special characters are considered all characters. A string is a group of these characters. 
  • Numbers – Any number from negatives to positives and decimals are also numbers.
  • Boolean – Boolean variables are values that are either true or false.
  • Undefined – A variable that isn’t defined or has no value at all.
  • Null – A variable that is empty and is meant to be empty.

This can be a bit confusing so let’s see what they look like. Open up your script.js file and replace it with this bit of code.

When it comes to strings, you must wrap them in quotes. This can be single quotes or double quotes.  The reason is because if you leave out quotes, then JavaScript will assume you’re calling a function or a variable. By using quotes, JavaScript understands that this line of code is a string. You can actually use quotes inside your quotes as long as they don’t  mix with each other. For example, if you use double quotes to wrap your string, then you can use as many single quotes as you want inside your string. However, if you try using double quotes, then you actually break your string and this can break your code.

Boolean values are either true of false. You use these kinds of values when you need to do conditional checks. You’ll see boolean values in action when we discuss conditional statements in a future tutorial.

Dynamic Data Types

Variables don’t have to be a certain data type forever. You can overwrite a variable’s data type and value. Here’s how you would do that. Add this bit of code in your script.js file.

This is what’s called dynamic data types. JavaScript allows you to change the data types anytime in a program. You don’t have to define a certain data type. Since JavaScript supports dynamic data types, it’ll automatically detect the data type for you and properly update it for you.

Conclusion

Knowing what data type your variables are is important. We’ll be using all the data types in this tutorial series. There are actually more data types such as arrays and objects, but those are a bit advance. We will be discussing what those are and how to use them in future tutorials. If you don’t fully understand how to use these data types, then don’t worry. We’ll be practicing with them a lot and you will understand their purpose in our programs.

About Jasko Koyn

Check Also

Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it …

Leave a Reply

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

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link