JavaScript Strings
A JavaScript string is a sequence of letters, numbers, special characters and arithmetic values or combination of all. JavaScript Strings can be created by enclosing the string literal i.e. string characters) either within single quotes (‘) or double quotes (“), as shown in the example below:
JavaScript Escape Sequences
Escape sequences are also useful for situations where you want to use characters that can’t be typed using a keyboard. Here are some other most commonly used escape sequences.
- \n is replaced by the newline character
- \t is replaced by the tab character
- \r is replaced by the carriage-return character
- \b is replaced by the backspace character
- \\ is replaced by a single backslash (\)
Getting the Length of a String
Finding a String Inside Another String
You can use the indexOf() method to find a substring or string within another string. This method returns the index or position of the first occurrence of a specified string within a string.
Similarly, you can use the lastIndexOf() method to get the index or position of the last occurrence of the specified string within a string, like this:
Both of the indexOf(), and the lastIndexOf() methods return -1, if the subtracting is not found.
Searching for a Pattern Inside a String
You can use the search() method to search a particular piece of text or pattern inside a string.
Like indexOf() method the search() method also returns the index of the first match, and returns -1 if no matches were found, but unlike indexOf() method this method can also take a regular expression as its argument to provide advanced search capabilities.
Extracting a Substring from a String
You can use the slice() method to extract a part or substring from a string.
This method takes 2 parameters: start index (index at which to begin extraction), and an optional end index (index before which to end extraction),
like str.slice(startIndex, endIndex).
Extracting a Fixed Number of Characters from a String
JavaScript also provide the substr() method which is similar to slice() with a subtle difference, the second parameter specifies the number of characters to extract instead of ending index, like str.substr(startIndex, length).
If length is 0 or a negative number, an empty string is returned.
Converting a String to Uppercase or Lowercase
You can use the toUpperCase() method to convert a string to uppercase, like this:
Similarly, you can use the toLowerCase() to convert string to lowercase, like this:
Concatenating Two or More Strings
You can concatenate or combine two or more strings using the + and += assignment operators.
Accessing Individual Characters from a String
You can use the charAt() method to access individual character from a string, like str.charAt(index). The index specified should be an integer between 0 and str.length -1. If no index is provided the first character in the string is returned, since the default is o.
Strings can be treated like read-only arrays, and you can access individual characters from a string using square brackets ([]) instead of the charAt() method, as demonstrated in the following example:
Working with Numbers
JavaScript supports both integer and floating-point numbers that can be represented in decimal, hexadecimal or octal notation. Unlike other languages, JavaScript does not treat integer and floating-point numbers differently. All numbers in JavaScript are represented as floating-point numbers.
Extra large numbers can be represented in exponential notation e.g. 6.02e+23 (same as 6.02×10²³).
Numbers can also be represented in hexadecimal notation (base 16). Hexadecimal numbers are prefixed into 0x. They are commonly used to represents colors. For example:
Operating on Numbers and Strings
As you know from the previous chapters, the + operator is used for both addition and concatenation. So, performing mathematical operations on numbers and strings may produce interesting results. The following example will show you what happens when you add numbers and strings:
If you observe the above example carefully, you will find that the result of the last operation is not just a simple string concatenation, because operators with the same precedence are evaluated from left to right. That’s why, since variables x and y both are numbers they are added first then the result is concatenated with the variable z which is a string, hence final result is 30 + “30” = “3030”.
But, if you perform other mathematical operations like multiplication, division, or subtraction the result will be different. JavaScript will automatically
convert numeric strings (i.e. strings containing numeric values) to numbers in all numeric operations, as shown in the following example:
Moreover, if you try to multiply or divide numbers with strings that are not numeric, it returns NaN (Not a Number). Also, if you use mathematical operation, the result will also be NaN.
If you liked our content JavaScript Strings, then you will also like JavaScript Events
Leave a Reply