Try the examples below in the browser console. Modify each at least once and test the effect.
String literals and variables
String literals are denoted either with double or single quotes.
"Jane" 'John'
String variables are declared as any other variable and can be assigned a value from a literal or from another variable.
// define variable s1 and assign the value "Jane" to it let s1 = "Jane"; // define variable s2 and assign the value "John" to it // (the value is the same regardless of the type of quotes used) let s2 = 'John'; // define the variable s3 and assign the value of s1 to it let s3 = s1;
ACTION: Try out the instructions in the two code listings above by running them in the console. After declaring a variable you can check its value by typing its name (followed by Enter), e.g.
let s1 = "Jane"; s1
Accessing individual characters of a string and concatenation
The String type has many properties and methods (see e.g. here).
- length property
- Contains the length of the string in characters.
- charAt(i) method
- Extracts and returns the character at index i.
- [i] operator
- Same as charAt. Extracts and returns the character at index i.
- + operator i.e. concatenation operator
- Concatenates strings with strings or with other types converted to strings.
Expected output:
![](res/images/tutorials/str1_output.png)
![](res/images/tutorials/str1_output.png)
// define variable s1 and assign to it the string value "Jane Jensen" let s1 = "Jane Jensen"; // declare variable c0 and initialise with first character from string s1 // (the character position numbers i.e. indices start at 0) let c0 = s1.charAt(0); // declare variable c8 and initialise with ninth character from string s1 let c8 = s1[8]; // declare variable sLength and initialise with the length of string s1 let sLength = s1.length; // declare variable cLast and initialise with the last character of string s1 // (spend a moment thinking about why we use index sLength-1) let cLast = s1.charAt(sLength - 1); // log the various variables to the console, combining them with literals // (e.g. "Extracted: ") // the combination (concatenation) is performed with the use of the // concatenation operator (+) console.log("Extracted: " + c0 + ", " + c8 + ", " + cLast);
ACTION: Try out the instructions in the code listing above by running them in the console. Check what is contained in the variables along the way.
Extracting substrings
- slice(b,e) method
- Extracts a substring from the string, starting with index b and ending at index e. If e is left out, the slice includes the characters from index b to the end.
Expected output:
![](res/images/tutorials/str2_output.png)
![](res/images/tutorials/str2_output.png)
// declare variable s and assign to it the string "TU Dublin Tallaght Campus"; let s = "TU Dublin Tallaght Campus"; // extract a substring from string variable s, // starting with the fourth character (with index 3) and // ending just before the 10th character (with index 9); // assign the substring to variable nameOfCity let nameOfCity = s.slice(3, 9); // extract a substring from string variable s // starting with the 20th character (with index 19) and // running to the end of the string; // assign the substring to variable typeOfPlace let typeOfPlace = s.slice(19); // write the results to console, concatenating with explanatory text console.log("Name of city: " + nameOfCity); console.log("Type of place: " + typeOfPlace);
ACTION: Try out the instructions in the code listing above by running them in the console. Check what is contained in the variables along the way.