JavaScript – Variables and Datatypes

Variables are fundamentals concept of every programing language. They are used to allocate space in memory to store information in form of so called “data” so later by referring to variable we can get its value.

JavaScript – Variables and Datatypes

What is variable?

Variables are fundamentals concept of every programing language. They are used to allocate a space in memory of an operating system to store information in form of so called “data”, in which that information within the variable can be used over and over later on that code, program.

Variables: are boxes or containers which allocate space in memory of an operating system to store data, information within that space.

In JavaScript to use variable and to store data we need to declare that variable first, declaring a variable is essentially using the JavaScript programing syntax by using the var keyboard following by name of variable and then assigning or storing data to that variable.

Declaring Variables

See the Pen CodePen.

In example above we used var keyboard to declare a, b and c variables. However there are two other keywords let and cons to declare variables since ES2015 JavaScript version also known as ES6.

Now, we have 3 ways of declaring variables. We will discuss the difference and usages between the 3 later in this lesson.

Variable Assignment:

This is an important step of using variables in any programming language to assign a value or data to variable using = sign, so we can later use that variable within our code.

See the Pen CodePen.

In this example person is declared as variables where  John is assigned as value or data to this variable. Later we can refer to person variable to get it’s value.

Let’s summarize what we learned:

  • var is the JavaScript keyboard to declare a variable
  • person is the name of the variable
  • John is the data, information or value we stored within person variable

Great progress so far 👍, Let’s discuss in what form or type we can assign data to JavaScript variables?

What are datatypes in JavaScript?

Data and informations come in different form and type in programming languages. Below are all allowed types in which data can be presented and stored in JavaScript variables.

  1. number floating point number, decimal or integer
  2. string sequence of characters, text
  3. boolean either true or false values
  4. object used for rather complex data structures
  5. symbol to represent unique identifiers
  6. undefined declared variable has no value yet
  7. null non-existent, nothing

See the Pen CodePen.

So far, we learned great stuff 😃 let’s move on with other JavaScript fundamentals.

How to name JavaScript variables?

In JavaScript like any other programming language there are set of rules to follow while naming variables.

  • Names should begin with lowercase string
  • Names cannot begin with symbol, or containing any symbol
  • Names can only start with _ symbol
  • Names cannot begin with a number
  • Names can contain a mix of uppercase and lowercase characters or numbers

See the Pen CodePen.

There are many ways developers using for naming JavaScript variables. However, there is no right or wrong between them, rather its just personal preferences.

Naming best practice:

Most common ways of variable naming among developers are camelCase and under_score for variable names more one keyword.

See the Pen CodePen.