ES6 – Variables and Datatypes

ES6 – 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


    

var a
var b
var c

// Combining variables, separating with comma
var a, b, c

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.


    

var person = “John”

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

    

var age = 30 // number
var person = “John” // string
var designer = true | false // boolean
var vehicle = {type:”lexus”, year:”2018″} // object
var zodiac = Symbol(‘cancer’)
var dob; // undefined
var country = null // Null

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

    

// Allowed
var first_name = “John”
var lastName = “Doe”
var _dob = “jan 1, 1990”
var car1 = “lexus”

// NOT Allowed
var 1car = “lexus” // Error: Uncaught SyntaxError: Invalid or unexpected token
var last-name = “Doe” // Error: Uncaught SyntaxError: Unexpected token ‘-‘
var @handle = “instagram” // Error: Uncaught SyntaxError: Invalid or unexpected token

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.


    

var lastName = “Doe” // camelCase
var last_name = “Smith” // under_score

Below the surface of the machine, the program moves. Without effort, it expands and contracts. In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisibly below.

—Master Yuan-Ma, The Book of Programming