DEV Community

Cover image for Identifier In JavaScript
capscode
capscode

Posted on • Originally published at capscode.in

Identifier In JavaScript

Let's first see what an Identifier is

An Identifier is a user-defined name or a sequence of characters in the programming languages to define a variable, function, or property.
It can be any combination of characters, but the combination of characters has a certain boundary/ restriction.

In this article, we will see what an Identifier means in JavaScript and what all combinations of characters are allowed in JavaScript to define any variable, function, or property name.

An Identifier in JavaScript

JavaScript identifiers are case-sensitive and can contain Unicode letters, $, _, and digits (0-9), but may not start with a digit.

An identifier is not a String, as a String is data, while an identifier is part of your code. In JavaScript, there is no way to convert identifiers to strings, but sometimes it is possible to parse strings into identifiers.

We cannot use reserved keywords of JavaScript as an identifier.

Let's see some examples of valid identifiers

let name = "CapsCode"
let Name = "CapsCode"
//javascript variables are case sensitive

let firstName = "John" //any combination of characters

let _name="john" //can start with underscore

let first_name = "john" //can contain underscore

let name_="john" //can end with underscore

let $name = "john" //can start with dollar

let first$name = "john" //can contain dollar

let name$ = "john" //can end with dollar

let first123name = "john" //can contain number

let name123 = "john" //can end with number
Enter fullscreen mode Exit fullscreen mode

Let's see some examples of invalid identifiers

  1. JavaScript variables do not contain spaces.
let first name = "John" //SyntaxError
Enter fullscreen mode Exit fullscreen mode
  1. JavaScript variables do not start with numbers.
let 1qw = "Rahul" //SyntaxError
Enter fullscreen mode Exit fullscreen mode
  1. Can contain only $
let first#name = "john" //SyntaxError
let name#="john" //SyntaxError
let #name="john" //SyntaxError
Enter fullscreen mode Exit fullscreen mode
  1. do not start with the number
let 123name = "john" //SyntaxError
Enter fullscreen mode Exit fullscreen mode
  1. should not be reserved keywords
let switch = "john" //SyntaxError
let export = "john" //SyntaxError
let new = "john" //SyntaxError
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is a brief introduction to what an identifier is and the various ways to define/ create a valid identifier in JavaScript.

If you find this article useful, share this article. Someone could find it useful too. If you find anything technically inaccurate, please feel free to create an issue.

Hope it's a nice and informative read for you.
VISIT https://www.capscode.in/blog TO LEARN MORE...
See you in my next Blog article, Take care!!

Thanks,
CapsCode

Top comments (0)