3

I was watching a video about object literals in JS. Then the guy in the video did something like this:

var Facebook = {
    name: 'Facebook',
    ceo: {
        firstName: "Mark",
        favColor: "Blue"
    },
$stock: 110
};

My question is why is there a $ sign in front of stock? Is there a special meaning? or did he just use it for naming purpose only? I entered $ in console and got something like this: function $(selector, [startNode]) { [Command Line API] }

I understand that $ sign is used as a selector for JS libraries like JQuery, but what is it's significance in pure JS?

Mr. T
  • 356
  • 3
  • 17

2 Answers2

1

It's just a character. If you saw something in the console it's because some script loaded by that page assigned it a value and it was still in the global scope.

Sometimes (like with jQuery or Angular) it can get used by convention to denote that the value assigned to that variable or property is related to those libraries somehow.

In your example it's just the name of a property.

aaronjkrause
  • 809
  • 5
  • 7
0

The $ has no special meaning in JavaScript. It's just a valid variable name. See this answer.

Community
  • 1
  • 1
randomir
  • 17,989
  • 1
  • 40
  • 55
  • But can you explain why when you enter $ in console it returns a function!? – Anonymous May 20 '17 at 18:16
  • 2
    @Anonymous it's a debugging global that exists in the console only, as convenience for the verbose `document.querySelector()` – Patrick Roberts May 20 '17 at 18:18
  • 1
    Literally just google ["Command Line API"](https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference) – Patrick Roberts May 20 '17 at 18:19
  • @Anonymous, `$` exists as a shortcut for `document.querySelector()`, but only in browsers (the name probably being `$` due to historic reasons and people used to it from jQuery). Try typing `$` in NodeJS -- `ReferenceError: $ is not defined`. – randomir May 20 '17 at 18:26