JS Bling requirement

JS Bling requirement
0.0 0

#1

Hi,

How do we know whether or not the bling($) is required in a JS statement?
Some statements seem to not work without it, and others don’t work if i add it.


#2

The jQuery library uses it as the name of its main function so you don’t have to write jquery every time. If you use jQuery, then you use it a lot. If you don’t use jQuery, you don’t. It’s not part of JS, it’s just defined by jQuery.

You can define it yourself for something else if you want. That’s not a very good idea as it’s commonly understood to refer to jQuery. If you do that and use jQuery, it’ll break jQuery, because you’ll have redefined what $ is. Example:

function $(name) {
  return 'Hello, ' + name + '!';
}
> $('Dan')
'Hello, Dan!'

#3

The $ is also used in template literals. It allows you to write complex string statements without the use of the + operator.

Example

You could replace:

var name = "Randell"
var language = "JavaScript"
var myStr = "My name is " + name + ".  I enjoy helping others learn " + language + "."

with

var name = "Randell"
var language = "JavaScript"
var myStr = `My name is ${name}. I enjoy helping others learn ${language}.`;

which uses a template literal.