Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is dollar sign in JavaScript

Meet The Dollar Sign in JavaScript

As you progress through your journey in learning JavaScript, you've probably stumbled upon a character that seems out of place - the dollar sign ($). This symbol, while it might look a bit strange, is a valid identifier in JavaScript. Let's demystify it.

Decoding The Dollar Sign

In JavaScript, the dollar sign ($) is just another character that can be used in variable names. It’s just like any other letter and can be used as the first or subsequent character in a variable name.

For instance, consider the following code:

let $myVariable = "Hello, world!";

When you run this code, it will print: Hello, world!. Here, $myVariable is just a regular variable that we've assigned a string value to.

Special Use Cases Of The Dollar Sign

While the dollar sign can be used like any other character in variable naming, it does have some special use cases in JavaScript.

1. With jQuery

If you've worked with jQuery, a fast, small, and feature-rich JavaScript library, you've likely seen the dollar sign in action. This is because jQuery uses the dollar sign as a shorthand to access its functions.

For example:


In this example, $ is an alias for the jQuery function. The code waits until the document is ready, then it sets up a click handler on the button that will hide the button when clicked.

2. Template Literals

In modern JavaScript (ES6 and later), the dollar sign precedes the curly braces ({}) within a template literal to interpolate variables.

Here's an example:

let name = "Alice";
console.log(`Hello, ${name}!`);

This code will print: Hello, Alice!. The ${name} part is a placeholder where the value of the name variable is inserted.

The Dollar Sign: An Analogy

Imagine you are a detective, and the dollar sign ($) is a special key. This key can open any door (function or variable), but some doors (like jQuery functions or template literals) have been designed specifically for this key and offer more features or ease of use.

Conclusion: Embracing The Dollar Sign

The dollar sign ($) in JavaScript might seem alien at first, but as you can see, it's just another character that can be used in variable names. It has its unique roles in jQuery and template literals, making it a versatile tool in your JavaScript toolkit.

So, next time you see a lonely $ sign in your JavaScript code, don't be intimidated. Just like a good friend, it may be a little difficult to understand at first, but once you get to know it, it can make your life a lot easier.

Remember, every symbol in programming has a purpose. It's like a puzzle piece. Alone it may not mean much, but when combined with others in the right way, it helps to create a beautiful, functioning program. Happy coding!