Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Javascript. No Comments on Variables in Javascript
0
(0)

In this article you will learn about variables in JavaScript. You will look at what is a variable and how to create it along with examples.

 

A variable is named memory location to store data. JavaScript is a loosely typed (or dynamically typed) language i.e., there is no need to declare the data type of a variable. The type of a variable is dynamically decided at runtime based on the value assigned. A variable can be assigned any of the five primitive type values or it can refer an object.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


A variable can be local or global. A local variable is declared in a function using the keyword var. Variables declared without the var keyword is always global even if they are created in a function. Variables declared using the var keyword outside the functions are also global variables.

 

Naming Convention

 

Following are the naming conventions for variables in JavaScript:

  • Variable name cannot be a keyword.
  • Variable name must start with a letter or an underscore followed by zero or more letters, underscores or digits.
  • Variable name must not contain white spaces or punctuation marks.
  • JavaScript is case-sensitive. So, count and COUNT are different.
  • Even though use of $ sign as the beginning character of a variable name is supported by new versions of browsers, it is not recommended. 

 

Local Variable

 

Local variables are variables that are declared within a function. The general syntax for creating a local variable is:

var  variable-name = value;

 

The keyword var specifies that the variable is a local variable. Below are some examples for local variables:

var sum = 0;   //number

var msg = “Hello”;    //string

var flag = false;   //boolean

var month = ”Feb”;   //string

 

By default if no value is specified to a variable, it is treated as undefined.

 

Global Variables

 

A global variable is created without using the keyword var. The general syntax for creating a global variable is as shown below:

variable-name = value;

 

Below are some examples for global variables:

x = 1       //number

lang = “js”     //string

flag = true;     //boolean

 

If a variable is declared inside a function without using the var keyword, it is also treated as a global variable. If a variable is already declared outside the function, no new variable will be created.

 

typeof Operator

 

The typeof operator accepts a single operand either a value or a variable and returns its type as a string value. It returns “number”, “string” and “boolean” for values or variables of the type Number, String and Boolean respectively. It returns “object” for values or variables of the type object or null. For a variable which is not assigned a value, the typeof operator returns “undefined”.

 

The typeof operator can be used in situations where there is a need to be absolute about the type of a value of variable before proceeding further. The typeof operator either may include parentheses for the operand or may not. So, both typeof variable and typeof(variable) are same.

 

Below table shows the return value of typeof operator for different values:

 

Type

Return Value

String ‘string’
Number ‘number’
Boolean ‘boolean’
Function ‘function’
Object ‘object’
Array ‘object’
Null ‘object’
Undefined ‘undefined’

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *