No particular character identifies a variable in JavaScript as the dollar sign does in PHP. Instead, variables use the following naming rules:

  • A variable may include only the letters a-z, A-Z, 0-9, the $ symbol, and the underscore (_).

  • No other characters such as spaces or punctuation are allowed in a variable name.

  • The first character of a variable name can be only a-z, A-Z, $, or _ (no numbers).

  • Names are case-sensitive. Count, count, and COUNT are all different variables.

  • There is no set limit on variable name lengths.

And yes, you’re right, that is the $ sign there in that list. It is allowed by JavaScript and may be the first character of a variable or function name. Although I don’t recommend keeping the $ signs, it means that you can port a lot of PHP code more quickly to JavaScript that way.

String Variables

JavaScript string variables should be enclosed in either single or double quotation marks, like this:

greeting = "Hello there"
warning  = 'Be careful'

You may include a single quote within a double-quoted string or a double quote within a single-quoted string. But a quote of the same type must be escaped using the backslash character, like this:

greeting = "\"Hello there\" is a greeting"
warning  = '\'Be careful\' is a warning'

To read from a string variable, you can assign it to another one, like this:

newstring = oldstring

or you can use it in a function, like this:

status = "All systems are working"

Numeric Variables

Creating a numeric variable is as ...

Get Learning PHP, MySQL, and JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.