Skip to content

Commit 7ec39af

Browse files
String in JS
1 parent 31c0ba9 commit 7ec39af

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
// 36. String in JS
3+
4+
/*
5+
A string is a sequence of one or more characters that may consist of letters, numbers, or symbols.
6+
Strings in JavaScript are primitive data types and immutable, which means they are unchanging.
7+
8+
Ex: 'string' or "string esfadfdf" or ['a', 'b', 'c', 'd', 'e']
9+
*/
10+
11+
var str2 = 'string'
12+
var abc = "abcdefghijklmnopqrstuvwxyz";
13+
var esc = 'I don\'t \n know'; // \n new line
14+
console.log(str2 + ' ' + abc + ' ' + esc)
15+
console.log(abc.length) // 26 // string length
16+
17+
console.log(abc.indexOf("lmno")) // 11 // find substring, -1 if doesn't contain
18+
console.log(abc.lastIndexOf("lmno")) // 11 // last occurance
19+
console.log(abc.slice(3, 6)) // def // cuts out "def", negative values count from behind
20+
21+
console.log(abc.replace("abc","123")) // 123defghijklmnopqrstuvwxyz // find and replace, takes regular expressions
22+
console.log(abc.toUpperCase()) // ABCDEFGHIJKLMNOPQRSTUVWXYZ // convert to upper case
23+
console.log(abc.toLowerCase()) // abcdefghijklmnopqrstuvwxyz // convert to lower case
24+
console.log(abc.concat(" ", str2)) // abcdefghijklmnopqrstuvwxyz string // abc + " " + str2
25+
26+
console.log(abc.charAt(2)) // c // character at index: "c"
27+
console.log(abc[2]) // c // unsafe, abc[2] = "C" doesn't work
28+
console.log(abc.charCodeAt(2)) // 99 // character code at index: "c" -> 99
29+
console.log(abc.split(",")) // [ 'abcdefghijklmnopqrstuvwxyz' ] // splitting a string on commas gives an array
30+
console.log(abc.split("")) // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] // splitting on characters
31+
// console.log(128.toString(16)); // number to hex(16), octal (8) or binary (2)
32+
33+
34+

0 commit comments

Comments
 (0)