Javascript Basic – What is a Variable in Javascript

Javascript Variables

Just think about a pot that can hold anything you put. In javascript, anything means like a number, string, object, or even a function can be declared as a variable. Before calling a variable you have to declare a variable. In JavaScript, variables are declared with the keyword “var”. Regardless of the type of variable, it is declared with the “var” keyword. After that, you have to set a variable name. You can take anything as a variable name, subject to certain conditions. Like as-

There are 4 Ways to Declare a JavaScript Variable:

  • Using var
  • Using let
  • Using const
  • Using nothing

You can’t use Javascript reserved keyword as a variable name.

Example:

var delete = 'name';
var int = 33;

Here are some of the reserved keys of Javascript in MDN Documentation.

A variable name can start with a letter but not with a number and special character (!, @, #, %, ^, &, *, {,}) except ( _, $ ).

Example:

var name = 'Mahin';     	// Vallid
var age = 33;           	// Vallid
var ^address = "Dhaka"; 	// inVallid
var *class = 7;         	// Vinallid
var _height = '5 feet'; 	// Vallid
var @age = 33;          	// inVallid

The space between the names of the variables cannot be used. If you need a name with a space in the middle, you can use ‘_’ () in camelcase format or between two words. But space is not allowed in any way.

Example:

var My name = 'Mahin';      // inVallid
var my_age = 33;            // Vallid
var my-address = "Dhaka";   // inVallid
var myClass = 7;            // Vallid

Camel case Format

If your variable name is logically long then you can use camel case format to declare the variable name

Example:

var myFirstNumber = 27;
var mySecondNumber = 34;
var myThirdNumber = 56;

Data Type

In Javascript, there are two kinds of data types. Such as:

  1. Primitive Data Type
  2. Non-Primitive Data Type

Primitive Data Type

Values ​​are stored directly in a primitive type of data.

Number

This is a kind of usual decimal number. In JavaScript, the Number data type represents numeric values, including integers and floating-point numbers. Numbers are used extensively in JavaScript programming for arithmetic operations, comparisons, and more.

Example:

var a = 10;                 // Valid
var a = "10";               // inValid
var anotherNumber = 10.10   // Vallid

Extra:  Get Decimal from Hexadecimal and Octal Numbers

From Hexadecimal: Put ‘0x’ before the Hexadecimal number. From Octal: Put ‘0’ before the Octal number.

Example:

var hex = 0xff
var oct = 0734;
 
console.log(hex);
console.log(oct);

Output:

255
476

String

It includes any type of character and sequence of characters. The string must be taken under either ( ‘ ’ ) or ( “ “ ).

Example:

var name = "Rabeya";        // Vallid
var name = Rabeya;          // inVallid
var anotherName = "Jenny"   // Vallid

Boolean

This type can have two values. True or false, but in lowercase

Example:

var isTrue = true;      // Vallid
var isTrue = True;      // inVallid
var isTrue = TRUE;      // inVallid
var isFalse = false;    // Vallid
var isFalse = False;    // inVallid
var isFalse = FALSE;    // inVallid

Undefined

When you declared a variable but don’t assign any value to it then the variable is undefined by default.

Example:

var name;
var something;

null

It does not exist but it is not also undefined. That means you declare a variable but don’t want to assign any value to it then assign ‘ null ’ to it.

Example:

var something = null;

Concatenation

concatenation is the process of combining two or more strings into a single string. Concatenation is a basic and commonly used operation in JavaScript, used in many programming tasks such as generating dynamic web pages or building text output. The + operator is used to concatenate two or more strings. When concatenating strings, the original strings are not modified; rather, a new string is created that contains the concatenated result.

Concatenation of string

In Javascript, If you want to concatenate any two strings just put ‘+’ ( plus ) in between them.

Example:

var text = 'How' + 'are you ?';    //But we have to put a space between ‘How’ and ‘are’
var text = 'How' + ' ' + 'are you ?';

Concatenation of number

Instead of string, If you want to concatenate any two numbers just put ‘+’ ( plus ) in between them.

Example:

var number = 10 + 20;
console.log(number);

Output:

30

Concatenation of both string and number

If you want to concatenate any string and number then put ‘+’ ( plus ) in between them. The result will always be a string.

Example:

var number = 10 + 'Hello';
console.log(number);

Output:

10Hello

 Variable Data Update

You can change your data of declared variables easily. This time just write your variable name and then assign your data to it.

Example:

var number = 10;
console.log(number);
	
var number = 15;
console.log(number);

Output:

10
15

Comment Writing

Writing comments in script or code is a good practice. Sometimes we have to take note of our work purpose. Since Javascript is an interpreted language so we have to comment it down So that the line cannot be interpreted.

Single-Line Comment

Example:

var myName = 'John';    //your comment is here.
 
// Write your name
var myName = 'David';
 
// var number = 34;

Multi-Line Comment

Example:

/*
var myName = 'John';   
var myName = 'David';
*/
var number = 34;
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.