Define Lines in a String
By Stephen Bucaro
You know that to define a String in Java Script requires that you
enclose the data within quotes. For example, the statement below displays
a string (the first line in Abraham Lincoln's in the Gettysburgh address)
in a message box.
alert("Four score and seven years ago our fathers brought forth on this
continent, a new nation, conceived in Liberty, and dedicated to the
proposition that all men are created equal.");
(code required to be on one line.)
As you can see above, this creates an unappealing too wide message box.
We can improve the aspect ratio of the message box by placing newline \n
character escape sequences within the string to explicitly define where
the string should break to create a new line, as shown below.
alert("Four score and seven years ago our fathers\n brought forth on this
continent, a new nation,\n conceived in Liberty, and dedicated to the\n
proposition that all men are created equal.");
(code required to be on one line.)
This creates the more appealing message box shown above.
• If you want to format your code so that it has the same line
structure as that displayed in the message box, enclose each line in quotes and
use the string concatenation operator to rejoin the lines, as shown below.
alert("Four score and seven years ago our fathers\n" +
"brought forth on this continent, a new nation,\n" +
"conceived in Liberty, and dedicated to the\n" +
"proposition that all men are created equal.");
This makes your code more neat and more understandable.
More Java Script Code: • Accessing Web Page Elements • Java Script Math.cos Method • Convert a String to a Number • The do/while Loop • Java Script prompt Message Box • Determine Absolute Value • Java Script Math.sin Method • Java Script Trigonometric Methods • Use moveBy Method to Move Window by a Specific Offset • Determine Minimum and Maximum
|