Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
What are the variable naming conventions in JavaScript ?
Next article icon

What characters are valid for JavaScript variable names ?

Last Updated : 23 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see the valid variable names in JavaScript. The valid variable names can contain letters (both uppercase and lowercase), numbers, underscores, and dollar signs. However, the variable names can not begin with numbers.

Some examples of valid variable names:

var Geeks;  var Geeks_for_Geeks;  var $Geeks;  var _Geeks;  var Geeks123;

Some examples of invalid variable names:

var Geeks-for-Geeks;  // Hyphens not allowed  var 123Geeks;  // Can not start with numbers  var Geeks for Geeks;  // Spaces not allowed

Note: The variable name can not be the same as JavaScript keywords, such as if, else, var, function, etc.

Rules for variable name:

  • Spaces are not allowed in variable names.
  • Hyphens are not allowed in variable names.
  • Only letters, numbers, underscores, and dollar signs are permitted in variable names.
  • Case matters when it comes to variable names.
  • Variable name always starts with a letter (alphabet), underscore (_), or a dollar sign ($), i.e. any other special characters not allowed.
  • Reserved keywords are not allowed.

Example 1: In this example, we will declare some variables with their values and print their sum on the console.

JavaScript
// Declare variable and assign value let Geeks1 = 15; let G$$ks = 20; let Geeks_for_geeks = 30;  // Add numbers const sum = Geeks1 + G$$ks + Geeks_for_geeks;  // Print the result console.log(sum); 

Output:

65

Example 2: In this example, we will declare some variables with values and print their values on the console.

JavaScript
// Declare variables and assign value let _Geeks1 = 15; let $Geeks2 = 20; let __Geeks3 = 30; let $$Geeks4 = 40;  // Print the result console.log(_Geeks1); console.log($Geeks2); console.log(__Geeks3); console.log($$Geeks4); 

Output:

15  20  30  40

Example 3: In this example, we will declare some invalid variables with their values and print them on the console, it displays some error message..

JavaScript
// Declare variables and assign value let @Geeks = 15; let Geeks-for-Geeks = 30; let Geeks for Geeks = 40;  // Print the result console.log(@Geeks); console.log(Geeks-for-Geeks); console.log(Geeks for Geeks); 

Output:

SyntaxError: Invalid or unexpected token

Next Article
What are the variable naming conventions in JavaScript ?

V

vkash8574
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • What are the variable naming conventions in JavaScript ?
    When we name variables in javascript, we've to follow certain rules. Each variable should have a name that appropriately identifies it. Your JavaScript code gets easier to comprehend and work with when you use suitable variable names. It's critical to name variables correctly. For example Constants
    2 min read
  • What are undeclared and undefined variables in JavaScript?
    Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using the var or const keyword. If we use 'typeof' operator to get the value of
    1 min read
  • How to Create a String with Variables in JavaScript?
    To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches: 1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with va
    2 min read
  • How to create a private variable in JavaScript ?
    In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari
    3 min read
  • How to create a variable using a user-defined name in JavaScript ?
    In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6
    2 min read
  • What is Variable Scope in JavaScript ?
    Variable scope is the context of the program in which it can be accessed. In programming, a variable is a named storage location that holds data or a value. Think of it as a container that you can use to store and manipulate information in your code. Variables allow you to work with data in a flexib
    4 min read
  • What are valid values for the id attribute in HTML?
    The id attribute is a unique identifier which is used to specify a document. The id attribute is used using #(hash) symbol followed by id. The value must be unique amongst all the IDs in the element's home subtree. Syntax: <tag id = #Values> Permitted values for ID attribute: As of HTML5, id m
    3 min read
  • How to create Static Variables in JavaScript ?
    To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth
    2 min read
  • Why JavaScript Variable Starts with a Dollar Sign ?
    In JavaScript, it is common to see variables that start with a dollar sign ($). This convention is not required by the language, but it is often used by developers to indicate that a variable is a jQuery object or to distinguish it from other types of variables. In JavaScript, variable names can onl
    3 min read
  • Which characters are valid in CSS class names/selectors?
    It is very easy to choose a valid class name or selectors in CSS just follow the rule. A valid name should start with an underscore (_), a hyphen (-) or a letter (a-z)/(A-Z) which is followed by any numbers, hyphens, underscores, letters.It cannot start with a digit, starting with the digit is accep
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences