It Spring Edutech: Introduction To The Javascript Strings
It Spring Edutech: Introduction To The Javascript Strings
console.log(message);
Code language: JavaScript (javascript)
Output:
Hello John
Code language: JavaScript (javascript)
The string message evaluates the name variable and returns the result
string.
Escaping special characters
Char[]={1 2 3 5 3 6 5 4 6 4 5 };
Accessing characters
The following example returns the first character of a string with the
index zero:
To access the last character of the string, you use the length - 1 index:
let str = "Hello";
console.log(str[str.length -1]); // "o"
Code language: JavaScript (javascript)
console.log(className);
Code language: JavaScript (javascript)
Output:
String(n);
”+n
n.toString()
Note that the toString() method doesn’t work for undefined and null.
When you convert a string to a boolean, you cannot convert it back via
the Boolean():
let status = false;
let str = status.toString(); // "false"
let back = Boolean(str); // true
Code language: JavaScript (javascript)
In this example:
Note that only string for which the Boolean() returns false, is the empty
string (”);
Comparing strings
To compare two strings, you use the operator >, >=, <, <=,
and == operators.
However:
Summary
The String type is object wrapper of the string primitive type and can be
created by using the String constructor as follows:
The String type has a property named length that specifies the number of
characters in the string.
console.log(str.length); // 22
Code language: JavaScript (javascript)
In this example, the value of the length property is 22 that also is the
number of characters in the string 'JavaScript String Type'.
To get the primitive string value, you use one of the following
methods of the string object: valueOf(), toString(), and toLocaleString().
console.log(str.valueOf());
console.log(str.toString());
console.log(str.toLocaleString());
Code language: CSS (css)
console.log(str[0]); // J
Code language: JavaScript (javascript)
The square bracket notation was introduced since ES5. Prior to ES5,
you use the charAt() method, which is more verbose:
console.log(str.charAt(0)); // J
Code language: JavaScript (javascript)
When you call a method on a primitive string variable or a literal
string, it is converted to an instance of the String type. For example:
'literal string'.toUpperCase();
Code language: JavaScript (javascript)
String manipulation
The String type provides many useful methods for manipulating strings
effectively. We will examine each of them in the following section.
1) Concatenating strings
Besides the concat() method, JavaScript also uses the addition operator
(+) for concatenating strings. In practice, the addition operator is used
more often than the concat() method.
2) Extracting substrings
The first argument startIndex is the location at which the characters are
being extracted, while the second argument length specifies the number
of characters to extract.
If you omit the length argument, the substr() method extracts the
characters to the end of the string.
substring(startIndex,endIndex)
3) Locating substrings
string.indexOf(substring,[fromIndex]);
Code language: CSS (css)
string.lastIndexOf(substring,[fromIndex])
Code language: CSS (css)
console.log(str.lastIndexOf('is')); // 5
Code language: JavaScript (javascript)
The lastIndexOf() method also returns -1 if the substring not found in the
string as shown in this example:
console.log(str.lastIndexOf('are')); // -1
Code language: JavaScript (javascript)