$ norms/best practices

Javascript course wanderlust project: https://www.codecademy.com/courses/introduction-to-javascript/projects/wanderlust

In the project I notice that several variable names being with $:

// Page Elements
const $input = $('#city');
const $submit = $('#button');
const $destination = $('#destination');
const $container = $('.container');
const $venueDivs = [$("#venue1"), $("#venue2"), $("#venue3"), $("#venue4")];
const $weatherDiv = $("#weather1");
const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

I know that _ before a variable name generally denotes that it isn’t meant to be edited directly. What does the $ denote in this context?

The $ is an alias for jQuery.

$('#city')

is a jQuery selector for the element with id="city".

It is the abstracted form of,

city = document.getElementById('city')

or the more recent,

city = document.querySelector('#city')

That some variables are prefaced with $ indicates they refer to jQuery objects. It has no technical importance, just visual.


Whenever you see this sort of nomenclature, check the HTML for a script load from a CDN of jQuery,

<script
  src="https://code.jquery.com/jquery-3.2.1.min.js" // ...
1 Like