JavaScript Tutorial #04 Variables

Posted at: May 7, 2019 2:57 PM

In this lesson we will learn variables in JavaScript.

In JavaScript, variable is a symbolic name or an identifier. Which contains some information referred as a value.

In JavaScript variables can be defined using either the var, let or const keywords. In this lesson we will discuss only var keyword. let or const keywords we will discuss in further lesson.

Rules for Declare Variables

Keep in mind some rules of declare variables in JavaScript

  • JavaScript variable name must be unique
  • JavaScript variable name can contains letters, digits, and underscores ( _ )
  • JavaScript variable name must be start with letters and underscore ( _ )
  • JavaScript variable name is case-sensitive (phone and Phone are different variables)
  • JavaScript reserved keywords var boolean or var typeof can not be used as variable names.

Valid variable names

Invalid variable names

Variables may hold values that have different data types.

String: sequence of text know as string. You must enclose it in single or duble quotes. For example var productName = 'Watch';

Number: A numbers have don not quotes. For example var quantity = 100;

Boolean: A true or false value. true and false are specials keywords, don't need quotes. For example var isActive = true;

Array: You store multiple value in single variable. For example var customer = [5, 'Sagar', 'Gaur', 1234567890, ''];

Object: Everything in JavaScript is an object, and can be stored in a variable. The values are written as name : value. For example var product = {name: "Watch", price: 500, qty: 50, sku: "watch01"};

var xyz;

A variable declared without a value will have the value undefined.

The xyz variable will have undefined value.

You can also declare variable in one statement.


In this lesson we have learnt, rules of declare variable, variable declare in one statement, valid and invalid variable name in the JavaScript.

This lesson also available on YouTube
javascript array javascript object javascript variable types javascript variable in string

Please leave comments