Data Types
In programming languages you'll always be working with different types of data. For example you could want to do some calculations with numbers or manipulate text in some way.
There are about 6 data types in JavaScript but lets stick to integers (numbers) strings (texts) and booleans ('yes or no') for now.
Integers
Integers are numerical values.
Some examples:
let visitorCount = 837;
const PI = 3.141;
let year = 2018;
I hope I don't need to explain why integers are important or when you'd need to use them, as it should be pretty obvious.
Please note that in JavaScript all numbers are floats (decimals).
At this point this is not really important but you should never forget it.
Strings
Strings represent text.
Some examples:
const website = 'JS Bible';
const author = 'Rodrigo';
const url = 'https://jsbible.com';
const info = 'Share the site with your friends!';
Booleans
A boolean is one of the simplest data types there are. They represent a logical value which can either be true or false.
Every day examples which you could store in a boolean could be things like:
- Is the light turned on?
- Is it raining outside?
- Is the earth flat..?
Some examples:
let userIsLoggedIn = false;
let userIsPremium = false;
let jsIsCool = true;
// Using ! as a boolean operator
console.log(userIsLoggedIn) // false
console.log(!userIsLoggedIn) // true
console.log(!!userIsLoggedIn) // false
You'll need booleans to do specific actions depending on specific conditions.
Let's say you have the same login interface of the strings section above. When a user sends a login request you might want to check if there was actually a value inserted in the e-mail and the password fields and only send the request to your server if there was.
It would make no sense to send the request with empty data, right?
JavaScript is untyped!
JavaScript is an untyped programming language, which means that a variable can hold different types of data in its lifecycle.
There is no type identifier for a variable as in other programming languages. A good example would be Java.
Comparable code would throw an error in Java while it perfectly works in JavaScript:
// Store an integer inside the variable
let number = 1;
// Store a string inside the variable
number = 'number';