1/*
2charAt() method
3This method takes in a string and an index as input
4and returns the character at that position in the specified string
5*/
6
7myString = "Hello World!";
8
9console.log(myString.charAt(0)); // outputs "H"
10console.log(myString.charAt(5)); // outputs " "
11console.log(myString.charAt(8)); // outputs "r"
12// If the specified index is bigger or equal to the length of the string,
13// the output will be "".
14console.log(myString.charAt(23)); // outputs ""
1let str = new String("This is string");
2console.log("str.charAt(0) is:" + str.charAt(0));
3console.log("str.charAt(1) is:" + str.charAt(1));
4console.log("str.charAt(2) is:" + str.charAt(2));
5console.log("str.charAt(3) is:" + str.charAt(3));
6console.log("str.charAt(4) is:" + str.charAt(4));
7console.log("str.charAt(5) is:" + str.charAt(5));
8
9output:
10
11str.charAt(0) is:T
12str.charAt(1) is:h
13str.charAt(2) is:i
14str.charAt(3) is:s
15str.charAt(4) is:
16str.charAt(5) is:i
1myString = "Hello World!";
2
3console.log(myString.charAt(0)); // outputs "H"
4console.log(myString.charAt(5)); // outputs " "
5console.log(myString.charAt(8)); // outputs "r"
6/* If the specified index is bigger or equal to the length of the string
7 the output will be "". */
8console.log(myString.charAt(23)); // outputs ""