Strings
String Literal Syntax
A string literal is zero or more characters enclosed in double (") or single (') quotes.
Note: For C/C++ programmers: Except for the use of single quotes, string literals have the same syntax as in C and C++.
Here are examples of string literals:
"My name is Hal"
'My name is Hal'
'"Hi there", he said'
"3.14"
"Hello, world\n"
In these examples, the first and the second strings are identical.
The backslash character (\) can be used to introduce an escape sequence, which stands for a character which cannot be directly expressed in a string literal. Escape sequences allowed in strings are:
Script Escape Sequences 
Escape Sequence
Stands for
\n
Newline
\t
Tab
\\
Backslash character (\)
\"
Double quote (")
\'
Single quote (')
\b
Backspace
\f
Form feed
\r
Carriage return
\xhh
The character whose ASCII code is hh, where hh is a sequence of two hexadecimal digits.
\ooo
The character whose ASCII code is ooo, where ooo is a sequence of one, two, or three octal digits.
Here are examples of string literals using escape sequences:
Script Escape Sequence Examples 
String Literal
Stands for
"Read \"The Black Bean\""
Read "The Black Bean"
'\'Hello\', he said'
'Hello', he said
"c:\\temp"
c:\temp
"First line\nSecond line\nThird line"
First line
Second line
Third line
"\xA9 1995-1997"
© 1995-1997
When a string is converted to a number, an attempt is made to parse it as a number literal. If the string does not represent a valid number literal, the conversion yields NaN.
Automatic Conversion to a String
When a function or a method which expects a string as one of its arguments is passed a non-string value, this value is automatically converted to a string. For example, if the string method indexOf is passed a number as its first argument, this number is treated like its string representation:
"The 10 commandments".indexOf(10) −> 4
Similarly, operators which take string operands automatically convert non-string operands to strings:
"The " + 10 + " commandments" −> "The 10 commandments"
The conversion to a string uses the toString method of the given value. All built-in values have a toString method.
String Properties
Strings have the following properties:
Script String Properties
Syntax
Value
string.length
Number of characters in string. This is a read-only property.
Examples
  "abc".length −> 3
  "".length −> 0
String Methods
Characters in a string are indexed from left to right. The index of the first character in a string string is 0, and the index of the last character is string.length-1.
Strings have the following methods:
Script String Methods 
Syntax
Effect
string.substring
(
start [ , end ] )
Returns the substring of string starting at the index start and ending at the index end-1. If end is omitted, the tail of string is returned.
Examples:
  "0123456".substring(0, 3) −> "012"
  "0123456".substring(2, 4) −> "23"
  "0123456".substring(2) −> "23456"
string.charAt(index)
Returns a one-character string containing the character at the specified index of string. If index is out of range, an empty string is returned.
Examples:
  "abcdef".charAt(0) −> "a"
  "abcdef".charAt(3) −> "d"
  "abcdef".charAt(100) −> ""
string.charCodeAt(index)
Returns the ASCII code of the character at the specified index of string. If index is out of range, return NaN.
Examples:
  "abcdef".charCodeAt(0) −> 97
  "abcdef".charCodeAt(3) −> 100
  "abcdef".charCodeAt(100) −> NaN
string.indexOf(substring
[ , index ] )
Returns the index in string of the first occurrence of substring. String is searched starting at index. If index is omitted, string is searched from the beginning. This method returns -1 if substring is not found.
Examples:
  "abcdabcd".indexOf("bc") −> 1
  "abcdabcd".indexOf("bc", 1) −> 1
  "abcdabcd".indexOf("bc", 2) −> 5
  "abcdabcd".indexOf("bc", 10) −> -1
  "abcdabcd".indexOf("foo") −> -1
  "abcdabcd".indexOf("BC") −> -1
string.lastIndexOf
(
substring [ , index ] )
Returns the index in string of the last occurrence of substring. String is searched backwards, starting at index. If index is omitted, string is searched from the end. This method returns -1 if substring is not found.
Examples:
  "abcdabcd".lastIndexOf("bc") −> 5
  "abcdabcd".lastIndexOf("bc", 5) −> 5
  "abcdabcd".lastIndexOf("bc", 4) −> 1
  "abcdabcd".lastIndexOf("bc", 0) −> -1
  "abcdabcd".lastIndexOf("foo") −> -1
  "abcdabcd".lastIndexOf("BC") −> -1
string.toLowerCase( )
Returns string converted to lowercase.
Examples:
  "Hello, World".toLowerCase() −> "hello, world"
string.toUpperCase( )
Returns string converted to uppercase.
Examples:
  "Hello, World".toUpperCase() −>
      "HELLO, WORLD"
string.split(separator)
Returns an array of strings containing the substrings of string which are separated by separator. See also the array method join.
Examples:
"first name,last name,age".split(",") −> an array a such that a.length is 3, a[0] is "first name", a[1] is "last name", and a[2] is "age".
If string does not contain separator, an array with one element containing the whole string is returned.
Examples:
  "hello".split(",") −> an array a such that a.length is 1 and a[0] is "hello",
string.toString( )
Returns the string itself.
String Functions
The following functions operate on strings:
Script String Functions 
Syntax
Effect
String.fromCharCode
(
code)
Returns a single character string containing the character with the given ASCII code.
Examples:
  String.fromCharCode(65) −> "A"
  String.fromCharCode(0xA9) −> "©"
parseInt(string
[ ,
base ] )
Parses string as an integer written in the given base, and returns its value. If the string does not represent a valid integer, NaN is returned.
Leading white space characters are ignored. If parseInt encounters a character that is not a digit in the specified base, it ignores it and all succeeding characters and returns the integer value parsed up to that point.
If base is omitted, it is taken to be 10, unless string starts with 0x or 0X, in which case it is parsed in base 16, or with 0, in which case it is parsed in base 8.
Examples:
  parseInt("123") −> 123
  parseInt("-123") −> -123
  parseInt("123.45") −> 123
  parseInt("1001010010110", 2) −> 4758
  parseInt("a9", 16) −> 169
  parseInt("0xa9") −> 169
  parseInt("010") −> 8
  parseInt("123 poodles") −> 123
  parseInt("a lot of poodles") −> NaN
parseFloat(string)
Parses string as a floating-point number and return its value. If the string does not represent a valid number, NaN is returned.
Leading white space characters are ignored. The string is parsed up to the first unrecognized character. If no number is recognized, the function returns NaN.
Examples:
  parseFloat("-3.14e-15") −> -3.14e-15
  parseFloat("-3.14e-15 poodles") −> -3.14e-15
  parseFloat("a fraction of a poodle") −> NaN
String Operators
The following operators can be used to manipulate strings:
Script String Operators 
Syntax
Effect
string1 + string2
Returns a string containing the concatenation of string1 and string2.
Examples:
  "Hello," + " world" −> "Hello, world"
When the operator + is used to add a string to a non-string value, the non-string value is first converted to a string.
Examples:
  "Your age is " + 23 −> "Your age is 23"
  23 + " is your age" −> "23 is your age"
string1 == string2

string1 != string2
The operator == returns the boolean true if string1 and string2 are identical, and false otherwise. Two strings are identical if they have the same length and contain the same sequence of characters. The operator != is the converse of ==.
Examples:
  "a string" == "a string" −> true
  "a string" == "another string" −> false
  "a string" == "A STRING" −> false
  "a string" != "a string" −> false
  "a string" != "another string" −> true
When the operators == and != are used to compare a string with a number, the string is first converted to a number and the two numbers are compared numerically.
Examples:
  "12" == "+12" −> false
  12 == "+12" −> true
string1 < string2

string1 <= string2

string1 > string2

string1 >= string2
The operator < returns true if string1 strictly precedes string2 lexicographically, and false otherwise. The operator <= returns true if string1 strictly precedes string2 lexicographically or is equal to it, and false otherwise; and so on.
Examples:
  "abc" < "xyz" −> true
  "a" < "abc" −> true
  "xyz" < "abc" −> false
  "abc" < "abc" −> false
  "abc" > "xyz" −> false
  "a" > "abc" −> false
  "xyz" > "abc" −> true
Etc.
When one of these operators is used to compare a string with a non-string value, the non-string value is first converted to a string.
Examples:
  "2" >= 123 −> true
  123 < "2" −> false
When one of these operators is used to compare a string with a number, the string is first converted to a number and the two numbers are compared numerically.
Examples:
  "10" > "2" −> false
  10 > "2" −> true
Published date: 05/24/2022
Last modified date: 02/24/2022