January 13, 2021
Data Types and Variables in JavaScript

JavaScript – Data Types & Variables

In this chapter we would cover Data Types and Variables.

We would cover the following:

  1. JavaScript Data Types
  2. Variables in JavaScript
  3. Variable Scope
  4. Variable Names


1. JavaScript Data Types

JavaScript supports three types of primitive data types: Numbers, Strings and Boolean


Numbers: These are numeric values. For example 100, 34, 12,5. Note that JavaScript does not differentiate between integer and floating point data. All are represented as floating-points. The size is 64 bits

Strings: Used for text data. For example: “Learn JavaScript”, “My name is Kindson”

Boolean: This is data type for True or False values


There is also two trivial data types: null and undefined. We would use these more in later sections.


Composite Data

JavaScript also supports composite data type. This is made up of combination of two or more primitive types. The composite data type is called object.



2. Variables in JavaScript

You probably have heard of variables. All programming languages use variables. A variable is like a container to hold data. Hence you can assign values to variables. However before you can use a variable, you must declare it.

To declare a variable, you use the var keyword. The code below declare three variables.


<script type = "text/javascript">
   var score;
   var name;
   var address;


After you declare a variable, then you can assign a value to the variable. This is called initialization. A variable can also be initialized the same time it it declared. For example, the code below declares the variables and assigns them at the same time.


<script type = "text/javascript">
   var score = 58.7;
   var name "Solace Okeke";
   var address = "No 34 King Will Avenue";


Also note that JavaScript is an untyped language. This means that variables are not tied to any specific type. Therefore a particular variable can hold any type: be it a number, string or boolean. Again, a variable can start off with holding a String value and then later you can assign it a number.



3. Variable Scope in JavaScript

The concept of variable scope actually applies across programming languages.

So, what is a variable scope?

A variable scope is  the block of code where a variable is defined. There are two variable types of variable scopes in JavaScript: Local and Global.


Local Variable: A local variables is a variable that is only visible within the function where it is defined. Function parameters for example. Hence, local variables are not visible outside the block

Global Variable: A global variable is a variable that is available anywhere in the JavaScript program. Both inside and outside functions


Inside the body of a function, local variables take precedence over global variables. These means that if there is a global variable with the same name, then the local variable is used. Let’s take an example:


<title> Variable Scopes</title>
   <body onload = vascope();>   
      <script type = "text/javascript">
	    // Global variable
            var variable1 = "global variable";    
            function vascope( ) {
	       // Local variable
               var variable1 = "local variable";


In the above code, when you run this page, then “local variable” is displayed. This is because, the write statement was done inside the function.


4. Variable Names in JavaScript

You can choose names for variables in JavaScript. However, the following rules apply to naming variables:

a. Any of the JavaScript reserved words cannot be used as a variable name. Reserved words are also known as keywords and a list of them is given below.

b. Variable names must not start with a number eg 0-9. Variable names must begin with a letter or an underscore. For instance score32, _firstname, address.

c. Variable names in JavaScript are case-sensitive. Therefore the variable Score is not the same as score.


List of JavaScript Reserved Words

The table below provides a list of all JavaScript keywords.


0 0 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments

[…] JavaScript – Data Types & Variables […]

Would love your thoughts, please comment.x