Variables and Datatypes in JavaScript

Datatypes in JavaScript

There are majorly two types of languages. First, one is Statically typed language where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C, C++, Java. Other, Dynamically typed languages: These languages can receive different data types over the time. For example- Ruby, Python, JavaScript etc.

JavaScript is dynamically typed scripting language. That is, in javascript variables can receive different data types over the time. Datatypes are basically type of data that can be used and manipulated in a program.



JavaScript supports 3 primitive datatypes:

  • Numbers: 5, 6.5, 7 etc.
  • String: “Hello GeeksforGeeks” etc.
  • Boolean: true, false.

Except for the above three primitive data types, JavaScript also supports two trivial datatypes null and undefined and one composite datatype object. We will learn about these datatypes in details in further articles.

Variables in JavaScript

A variable is a name given to a memory location. It is the basic unit of storage in a program.



  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
  • In JavaScript, all the variables must be declared before they can be used.

To declare a variable in JavaScript, we must use the keyword var followed by the name of the variable and semi-colon. Below is the syntax to create variables in JavaScript:

var var_name;

The var_name is the name of the variable which should be defined by the user and should be unique. These type of names are also known as identifiers. The rules for creating an identifier in JavaScript are, the name of the identifier should not be any pre-defined keyword, the first character must be a letter, an underscore (_), or a dollar sign ($). Subsequent characters may be any letter or digit or an underscore or dollar sign.

We can initialize the variables either at the time of declaration or also later when we want to use them. Below are some examples of declaring and initializing variables in JavaScript:

// declaring single variable
var name;

// declaring multiple variables
var name, title, num;

// initializng variables
var name = "Harsh";
name = "Rakesh";

Javascript is also known as untyped language. This means, that once a variable is created in javascript using the keyword var, we can store any type of value in this variable supported by javascript. Below is the example for this:

// creating variable to store a number
var num = 5;

// store string in the variable num
num = "GeeksforGeeks";

The above example executes well without any error in JavaScript unlike other programming languages.

Variable Scope in Javascript

Scope of a variable is the part of the program from where the variable may directly be accessible.

In JavaScript, there are two types of scopes:



  1. Global Scope – Scope outside the outermost function attached to Window
  2. Local Scope – Inside the function being executed

Let’s look at the code below. We have a global variable defined in first line in global scope. Then we have a local variable defined inside the function fun().

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type = "text/javascript">
var globalVar = "This is a global variable";
  
function fun() {
  var localVar = "This is a local variable";
  
  console.log(globalVar);
  console.log(localVar);
}
  
fun();
< /script>

chevron_right


Output:

This is a global variable
This is a local variable

When we execute the function fun(), the output shows that both global as well as local variables are accessible inside the function as we are able to console.log them. This shows that inside the function we have access to both global variables (declared outside the function) and local variables (declared inside the function).Let’s move the console.log statements outside the function and put them just after calling the function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type = "text/javascript">
var globalVar = "This is a global variable";
  
function fun() { var localVar = "This is a local variable"; }
  
fun();
  
console.log(globalVar);
console.log(localVar);
< /script>

chevron_right


Output:

This is a global variable
Uncaught reference error: localVar is not defined

We are still able to see the value of the global variable, but for local variable console.log throws an error. This is because now the console.log statements are present in global scope where they have access to global variables but cannot access the local variables.
To understand variable scopes in details in JavaScript, please refer to the article on understanding variable scopes in Javascript.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

7


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.