You might get confused to see the dollar sign ($) in JavaScript coding. And wonder what does the dollar sign mean in javascript?

This guide will clear your confusion about the use of the dollar sign in JavaScript.

1. Dollar Sign in Template Literals

In JavaScript we use $ sign and then { } to embed expression in string. It allows multi-line string and string interpolation features.

Example 1: To Embed and evaluate an arithmetic express inside a string.


let string = `2 + 2 is ${2+2}`;

console.log(string);

Example 2: To Embed a value from a javaScript variable in a string.


let age = 25;
let message = `I am ${age} years old.`;

console.log(message);

If you use ” ” (double quote)  or ‘ ‘ (single quote) and try to use the ${expression} it won’t work.

Note: The template literals are enclosed by ` ` (backtick).

2. JavaScript Dollar Sign Variable

When using jQuery, developers usually prefer to use a variable name with a dollar sign. It’s a personal practice to identify that it contains a jQuery object.

Here is a code example of jQuery with a dollar sign variable.


// store a jQuery object
var $myDiv = $('#divID');  
// to create a jQuery object you must have the jQuery library


// or in plain JavaScript
var myDiv = Document.getElementByID('divID');

You might have seen something like the above code.

On line number 2,  where we have declared a variable $myDiv, it does not mean anything special to the JavaScript engine. It is the same as that of myDiv at line number 7.

In conclusion, If you are using jQuery with a mix of Plain Js then, it is a good practice to prefix the variable that contains a jQuery object with a dollar sign. This help in identification, understand the code better.

If you do so, a $ sign in a variable simply will remind you that it contains a jQuery object not a string, boolean or a number, etc.

Finally, if you have any confusion about the dollar sign in JavaScript then let us know in the comments below. Also, you might be interested to read about the Math floor JavaScript method.

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed.