JavaScript variables and Data types Bundle
Every programming language requires something to store information. Information which is required to handle and manipulate data. JavaScript is no new to this style.
Information is a collection of data. Information could be available to us in different forms. Some of the examples could be:
- A Family tree : Names of the family members, age, hierarchy etc. is an information
- Telephone Directory : Number of people in the list, their addresses and contact details are all collection of information in the directory.
JavaScript Variables
A variable can be assumed as a container for data. Suppose you have six apples. Now, you will need a box to hold them. This box is a variable in JavaScript which stores these 6 apples.
JavaScript allows it’s variables to store any kind of data. Programming languages which allow this ease of access are called “Dynamically Typed”. This means a variable can hold a number or even strings without declaring explicitly.
Now we know what is a variable. The question arises how our system wil understand variables.Therefore JavaScript allows us three types of variable declarations.
var keyword : We have been using var in our quite a few times in our previous examples. Variables declared in var do not lose their value ie. they have global scope. Their scope is extended within the function boundaries or they are global.
let keyword : let keyword is also used to declare variables but scope of let is limited to its block. It’s value cannot be accessed outside its block. It is also declared the same way as var. We are not going into details for now. We will discuss them in detail further.
const keyword : Values declared using const keyword cannot be changed. If trying to do so, you will get an error. Values stored under const are constant “unchangeable”.
This is all we need to know about declaring variables in JavaScript for now. We will come to a detailed study on these variable declarations in our later discussions and see how they handle data.
JavaScript Data types
We have already discussed that JavaScript is “dynamically typed” if you have been paying attention! But in JavaScript there are basically seven Data types that are primitive. We are going to discuss them now and with further chapters we will see them in action.
Number
A number is all integers and floating point numbers. All kind of mathematical operations like addition, subtraction, multiplication and division can be performed with numbers.
String
A string is anything is JavaScript which is written in quotes. It could be either single or double quotes. JavaScript does not differentiate between them.
Boolean
A boolean has two values, either “true” or “false”. It checks whether the value or condition is correct or not. We will discuss about Boolean in details later.
Null
Null represents “empty” or “no value” in JavaScript.
undefined
undefined in JavaScript means “value is not defined”. Some specific functions may return undefined in JavaScript.
Objects
Objects are non primitive data types. Objects can be container for storing type of a data or even similar data within a single container. We do not need to go in details with objects for now.
typeof operator
typeof operator returns the type of data, as simple as this and can be used for a quick check. It’s syntax is typeof(variable name).
This was all about JavaScript variables and data types that we need to know about for now. Definitely this is not all but we will see them in action in further chapters and get a better understanding of their usage.
Till then Happy Learning!