JavaScript - Strings and Numbers and Concatenation:

Strings:

3 examples of strings:  "I love fish. So do cats."   "<b>Hi</b>"   "2006"

Some JavaScript commands (like prompt()) need strings to work, and some commands don't work with strings.


Quotation marks in strings:

Because strings are usually surrounded by double quotation marks (like the ones around "this"), it causes a problem if you want to have a quotation mark inside the string.

This is not a problem:

var hiswords = "He said he was hungry.";

BUT this IS a problem:
var hiswords = "He said, "I'm hungry!"";

There are several ways you could get around this problem:

- The most correct and elegant is to place a \ in front of every internal " mark, like so:
   var hiswords = "He said, \"I'm hungry!\"";
  The browser will understand that each \" pair indicates an internal quotation mark, not the quotation mark at the beginning or end of the string.

- A kludgey way around the problem is to replace every internal " mark with a single quote (an apostrophe), like so:
   var hiswords = "He said, 'I'm hungry!'";
   
   

Also see Mozilla Core JavaScript Reference - Strings


 

Numbers / Values:

Numbers may be referred to as values in JavaScript and other languages

Some Number and String Commands:


 

Concatenation:

How does JavaScript know when to concatenate with + and when to do math with +? Basically, JavaScript takes what you give it (string or number) as a precedent and then proceeds accordingly.

If you give JavaScript a string to work with, it will concatenate; if you give it only numbers, it will do math.

For example:

If you give JavaScript the line:
var x = 2;
and then you give it the line:
var y = 3 + x;
you would expect the variable y to equal 5, and it would!

BUT, if you give JavaScript the line:
var x = "2";
the 2 is not a number but a string, and if then you had the line:
var y = 3 + x;
the variable y would contain this string: "32"