JavaScript Introduction | Standard Objects | String Object
String Representation in JavaScript
In JavaScript, string literals can be created easily by using double quotation marks ("") or single quotation marks (’’).
var firstStr = "This is also a string."; // string using double quotation marks
var secondStr = 'This is also a string.'; // string using single quotation marks
var thirdStr = "My name is 'Hong Gil-dong'." // single quotation marks can be included only in strings enclosed in double quotation marks.
var fourthStr = 'My name is "Hong Gil-dong".' // double quotation marks can be included only in strings enclosed in single quotation marks.
String Length
In JavaScript, the length of a string is stored in the length property.
var strKor = "한글";
var strEng = "abcABC";
strKor.length; // 2
strEng.length; // 6
In the long-used ASCII encoding environment, English letters are represented as 1 byte per character, and Korean characters as 2 bytes per character.
However, in the UTF-8 encoding environment, English letters are represented as 1 byte per character, and Korean characters as 3 bytes per character.
The JavaScript length property stores only the number of characters, not the total number of bytes in the string.
Escape Sequence
JavaScript provides several escape sequence methods for more diverse character representation. The escape sequence methods provided by JavaScript are as follows.
- Hexadecimal escape sequence
- Unicode escape sequence
- Unicode code point escape
// In a hexadecimal escape sequence, what follows \x is recognized as a hexadecimal number.
'\xA2';
// In a Unicode escape sequence, what follows \u is recognized as Unicode.
'\u00A2';
// Unicode code point escape method newly added from ECMAScript 6.
String.fromCodePoint(0x00A2);
The String.fromCodePoint() method is not supported in Safari or Internet Explorer.
Splitting Long String Literals
In JavaScript, a backslash () or concatenation (+) operator can be used to make long string literals easier to read.
document.write("This is a very long string. \
Therefore, it needs to be split across several lines. \
In JavaScript, you can split lines using a backslash and the string concatenation operator.<br>");
document.write("This is a very long string." +
" Therefore, it needs to be split across several lines." +
" In JavaScript, you can split lines using a backslash and the string concatenation operator.");
The method using a backslash () is not an ECMAScript standard method. Therefore, it may not be displayed correctly in some web browsers.
String Object
In JavaScript, strings are usually represented using string literals.
However, when representing strings, you can explicitly create a String object with the new operator.
This String object is a wrapper object that wraps a string value.
var str = "JavaScript";
var strObj = new String("JavaScript");
str; // "JavaScript"
strObj; // "JavaScript"
typeof str; // string
typeof strObj; // object
(str == strObj); // string values are the same, so returns true.
(str === strObj); // string values are the same, but types differ, so returns false.