Identifier rules in JavaScript

What you will learn here about javascript identifier

  • What is identifier
  • JavaScript identifier rules

What is identifier

Basically identifier tells, how to define variable name or function name or array name or class name etc. In simple words identifier tell us what number, character or symbols we can use for defining or constructing variable name or function name or array name or class name or object name etc. in javascript.

JavaScript identifier rule

In javascript there are certain identifier rules that we need to follow while creating or defining variable name. JavaScript identifier rule are given below

1)Identifier can’t have space

Variable name should not have space while creating or defining variable name.

//This is allowed
var msg=”Hello World”;

//This is not allowed in javascript
var m sg=”Hello World”

2)Identifier can have numbers but can’t start with number

We can define variable name which can have number, characters etc but can’t start with numbers.

//This is allowed
var msg123=”Hello World”;

//This is not allowed in javascript
var 1msg=”Hello World”

3)Identifier can have only two symbols

Identifier can have only two symbols and those are $(Dollar) and _ (Under score). Identifier can also start with Symbols.

//This is allowed
var msg$=”Hello World”;

//This is also allowed
var _msg=”Hello World”;

//This is not allowed
var msg#=”Hello World”;

4)Identifier can’t be keyword

Keywords are reserved words whose meaning is already defined. So we can’t use keyword as variable name.

//This is allowed
var msg=”Hello World”;

//This is not allowed
var var=”Hello World”;

You may also like...