JavaScript Statements - How to Enter Special Characters
(Page 4 of 4 )
Before we go any further we should discuss the role of special characters in JavaScript. Special characters include apostrophes, new lines, double and single quotes, the ampersand, etc.
To insert any of those in JavaScript, you need to use the (/) backslash key.
<html>
<body>
<script type="text/javascript">
var description = “The Wookie said, “Rawr” and stabbed me.”
document.write(description)
}
</script>
</body>
</html>
I know, Wookies don't say Rawr. If anyone wants to take the time to spell the sound a Wookie makes, I will be glad to add it to the code. Until then though, good ole Chewbacca says Rawr.
Since Javascript reads double and single quotes as the beginning and end of a string, the above code would have caused this to print:
The Wookie said,
That's obviously not what we had intended. If left like that, millions of Star Wars nerds would be riveted to their seats, waiting to find out exactly what that giant Ewok really had to say. Which is fine really, because what else do they have to do?
To remedy this particular situation, we would simply input the backslash (/) into our code. Like this:
<html>
<body>
<script type="text/javascript">
var description = “The Wookie said, /“Rawr/” and stabbed me.”
document.write(description)
}
</script>
</body>
</html>
This would cause the following to print out:
The Wookie said, “Rawr” and stabbed me.
The backslash goes before each quote, transforming them into a string literal.
Below is a list of how to handle other special characters:
' : Outputs a single quote.
” : Outputs a double quote.
& : Outputs an ampersand
: Outputs a backslash
n : Outputs a new line
r : Outputs a carriage return
t : Outputs a tab
b :Outputs a backspace
f : Outputs a form feed
Before we say goodbye, I want to show you a big table full of operators. You can study it before our next tutorial if you like.
Arithmetic Operators
Operator Symbol | What it Does |
+ | Used for addition |
- | Used for subtraction |
* | Used for multiplication |
/ | Used for division |
% | Returns the remainder in division |
++ | Increments a value by 1 |
-- | Decrements a value by 1 |
Assignment Operators
Operator Symbol | What it Does |
“=” | Assigns a value to a variable |
+= | Assigns and adds a value |
-= | Assigns and subtracts a value |
*= | Assigns and multiplies a value |
/= | Assigns and divides a value |
%= | Assigns and Modulates a value |
Comparison Operators
Operator Symbol | What it Does |
“==” | Is equal to |
“===” | Is equal to both value and type |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or Equal To |
<= | Less than or Equal To |
Logical Operators
Operator Symbol | What it Does |
&& | AND |
|| | OR |
! | NOT |
That's it for this episode. In the next article we will talk more about Operators and how to manipulate data with the, learn how to work with Functions, and maybe even cover Functions.
Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |