|  | 
|  | 1 | +alert("Strings in JavaScript!"); | 
|  | 2 | + | 
|  | 3 | +/* | 
|  | 4 | + JavaScript Strings | 
|  | 5 | + Strings are for storing text | 
|  | 6 | + | 
|  | 7 | + Strings are written with quotes | 
|  | 8 | + | 
|  | 9 | + Using Quotes | 
|  | 10 | + A JavaScript string is zero or more characters written inside quotes. | 
|  | 11 | +*/ | 
|  | 12 | + // Example | 
|  | 13 | + let text = "John Doe"; | 
|  | 14 | + | 
|  | 15 | + // You can use single or double quotes: | 
|  | 16 | + | 
|  | 17 | + // Example | 
|  | 18 | + let carName1 = "Volvo XC60"; // Double quotes | 
|  | 19 | + let carName2 = 'Volvo XC60'; // Single quotes | 
|  | 20 | +/* | 
|  | 21 | + Note | 
|  | 22 | + Strings created with single or double quotes works the same. | 
|  | 23 | + There is no difference between the two. | 
|  | 24 | + | 
|  | 25 | + Quotes Inside Quotes | 
|  | 26 | + - You can use quotes inside a string, as long as they don't match the quotes surrounding the string: | 
|  | 27 | +*/ | 
|  | 28 | + // Example | 
|  | 29 | + let answer1 = "It's alright"; | 
|  | 30 | + let answer2 = "He is called 'Johnny'"; | 
|  | 31 | + let answer3 = 'He is called "Johnny"'; | 
|  | 32 | + | 
|  | 33 | +/*  | 
|  | 34 | + Template Strings | 
|  | 35 | + - Templates were introduced with ES6 (JavaScript 2016). | 
|  | 36 | + - Templates are strings enclosed in backticks (`This is a template string`). | 
|  | 37 | + - Templates allow single and double quotes inside a string: | 
|  | 38 | +*/ | 
|  | 39 | + // Example | 
|  | 40 | + let text1 = `He's often called "Johnny"`; | 
|  | 41 | +/* | 
|  | 42 | + Note: Templates are not supported in Internet Explorer. | 
|  | 43 | + | 
|  | 44 | + String Length | 
|  | 45 | + - To find the length of a string, use the built-in length property: | 
|  | 46 | +*/ | 
|  | 47 | + // Example | 
|  | 48 | + let text2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 
|  | 49 | + let length = text.length; | 
|  | 50 | +/* | 
|  | 51 | + Escape Characters | 
|  | 52 | + - Because strings must be written within quotes, JavaScript will misunderstand this string: | 
|  | 53 | + | 
|  | 54 | + let text3 = "We are the so-called "Vikings" from the north."; | 
|  | 55 | + | 
|  | 56 | + The string will be chopped to "We are the so-called ". | 
|  | 57 | + To solve this problem, you can use an backslash escape character. | 
|  | 58 | + The backslash escape character (\) turns special characters into string characters: | 
|  | 59 | + | 
|  | 60 | + Code	Result	Description | 
|  | 61 | + \'	'	Single quote | 
|  | 62 | + \"	"	Double quote | 
|  | 63 | + \\	\	Backslash | 
|  | 64 | + | 
|  | 65 | +*/ | 
|  | 66 | + // Examples | 
|  | 67 | + // \" inserts a double quote in a string: | 
|  | 68 | + let text4 = "We are the so-called \"Vikings\" from the north."; | 
|  | 69 | + | 
|  | 70 | + // \' inserts a single quote in a string: | 
|  | 71 | + let text5 = 'It\'s alright.'; | 
|  | 72 | + | 
|  | 73 | + // \\ inserts a backslash in a string: | 
|  | 74 | + let text6 = "The character \\ is called backslash."; | 
|  | 75 | + | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +// JavaScript String Methods | 
|  | 79 | + | 
|  | 80 | + let myString = 'I\'m a "fun" string'; | 
|  | 81 | + document.write("String: " + myString + "<br />"); | 
|  | 82 | + | 
|  | 83 | + // JavaScript String Length | 
|  | 84 | + // The length property returns the length of a string: | 
|  | 85 | + document.write("<br />" + "String Length: " + myString.length); | 
|  | 86 | + | 
|  | 87 | + // JavaScript Uppercase | 
|  | 88 | + document.write("<br />" + "String to uppercase: " + myString.toUpperCase()); | 
|  | 89 | + | 
|  | 90 | + // JavaScript Lowercase | 
|  | 91 | + document.write("<br />" + "String to lowercase: " + myString.toLowerCase()); | 
|  | 92 | + | 
|  | 93 | + // Javascript string find index | 
|  | 94 | + document.write("<br />" + "Word 'fun' index: " + myString.indexOf("fun")); | 
|  | 95 | + | 
|  | 96 | + // Javascript string find index thrw -1 for not found strings | 
|  | 97 | + document.write("<br />" + "Word 'apple' index: " + myString.indexOf("apple")); // This apple is not in myString string variable. | 
|  | 98 | + | 
|  | 99 | + if(myString.indexOf("apple") === -1){ | 
|  | 100 | + | 
|  | 101 | + document.write("<br />The word 'apple' is not in the string"); | 
|  | 102 | + | 
|  | 103 | + } else { | 
|  | 104 | + | 
|  | 105 | + document.write("<br />The word 'apple' starts at position" + myString.indexOf("apple")); | 
|  | 106 | + | 
|  | 107 | + } | 
|  | 108 | + | 
|  | 109 | + | 
|  | 110 | + let string1 = "abc"; | 
|  | 111 | + let string2 = "bcd"; | 
|  | 112 | + let string3 = "abc"; | 
|  | 113 | + let string4 = "ABC"; | 
|  | 114 | + | 
|  | 115 | + console.log(string1 === string2); | 
|  | 116 | + console.log(string1 === string3); | 
|  | 117 | + console.log(string1 === string4); | 
|  | 118 | + | 
|  | 119 | + console.log(string1 < string4); | 
|  | 120 | + console.log(string1 > string4); // In JavaScript Uppercase letters are smaller than lowercase letters. | 
|  | 121 | + | 
|  | 122 | + | 
0 commit comments