|
| 1 | +var myName = "Nitin" |
| 2 | +let yourName = "Ramesh" |
| 3 | +const pi = 3.14; // can never change |
| 4 | +// var - used accorss program |
| 5 | +// let - inside the block only |
| 6 | + |
| 7 | +var a; //declaring |
| 8 | +var b = 2; //declaraing + assigning |
| 9 | +a = b; //assigning |
| 10 | +console.log(a) |
| 11 | + |
| 12 | +a = a+1; |
| 13 | +myName = myName + " Hello" //The result of + is conact and - is NaN |
| 14 | +myName += " Hello " + yourName |
| 15 | +console.log(a, myName) |
| 16 | + |
| 17 | +a = a+1; |
| 18 | +a++; //increamenting |
| 19 | +a--; //decreamenting a = a-1 |
| 20 | + |
| 21 | +var dec = 0.3333; |
| 22 | +console.log( dec * 0.5) |
| 23 | + |
| 24 | +var remainder; |
| 25 | +remainder = 11 % 3; |
| 26 | +console.log(remainder) |
| 27 | + |
| 28 | +var div; |
| 29 | +div = 11 / 3; |
| 30 | +console.log(div) |
| 31 | + |
| 32 | +var myStr = "I am a very good boy. My Name is \"Ramesh\"." |
| 33 | +console.log(myStr) // \" - escaping the next character |
| 34 | +var myLink = `https://www.google.com/'"search?q=what+%26operator+does+in` |
| 35 | +// so if you use `` then you can use '' & "" inside it |
| 36 | +/* |
| 37 | +\\ - Backlash |
| 38 | +\n - newline |
| 39 | +\t - toolbar |
| 40 | +\b - backspace |
| 41 | +\r, \f |
| 42 | +*/ |
| 43 | + |
| 44 | +//indexing |
| 45 | +myStr = "Automation"; |
| 46 | +console.log(myStr); |
| 47 | +console.log(myStr[0]); // Prints first character |
| 48 | +console.log(myStr[myStr.length-1]); // Prints Last character |
| 49 | +console.log(myStr[myStr.length-3]); // Prints 3rd last character |
| 50 | + |
| 51 | +myStr[0] = 'H'; |
| 52 | +console.log(myStr); |
| 53 | +/* Strings in JavaScript are immutable. This means that once a string is created, its value cannot be changed. |
| 54 | +
|
| 55 | +### Key Points: |
| 56 | +1. **No Direct Modification:** If you try to change a character in a string, it won’t work. For example: |
| 57 | +```javascript |
| 58 | +let str = "hello"; |
| 59 | +str[0] = "H"; // Does nothing - It is doesn't change the existing value and it doesn't throw any error also. |
| 60 | +console.log(str); // Output: "hello" |
| 61 | +``` |
| 62 | +
|
| 63 | +2. **New String Creation:** Any operation that appears to modify a string actually creates a new string. For example: |
| 64 | +```javascript |
| 65 | +let str = "hello"; |
| 66 | +let newStr = str.toUpperCase(); // Creates a new string |
| 67 | +console.log(newStr); // Output: "HELLO" |
| 68 | +console.log(str); // Original remains "hello" |
| 69 | +``` |
| 70 | +
|
| 71 | +3. **Reason for Immutability:** |
| 72 | +- Strings are designed this way for performance optimization, especially since they are heavily used in programming. |
| 73 | +- Immutability helps with string interning and makes strings more predictable in concurrent or multi-threaded environments. |
| 74 | +
|
| 75 | +### If You Need Mutability: |
| 76 | +If you need to modify a string frequently, consider using a mutable structure like an **array**: |
| 77 | +```javascript |
| 78 | +let arr = ["h", "e", "l", "l", "o"]; |
| 79 | +arr[0] = "H"; |
| 80 | +console.log(arr.join("")); // Output: "Hello" |
| 81 | +``` |
| 82 | +*/ |
| 83 | + |
| 84 | +// ARRAY |
| 85 | +var myArray = [44, 33, 55]; |
| 86 | +console.log(myArray[0]); // To get the 1st element in the array |
| 87 | +console.log(myArray[myArray.length - 1]); // To get the last element in the array |
| 88 | + |
| 89 | +// Push - Pop |
| 90 | +myArray.push(88) // push() = Adds an element to end of the array |
| 91 | +// Array.push([66, 77, 88]) // Push another array |
| 92 | +console.log(myArray) |
| 93 | +var popped = myArray.pop(); // pop() = Removes an element to end of the array |
| 94 | +console.log(myArray) |
| 95 | +console.log(popped) |
| 96 | + |
| 97 | +// Shift - Unshift |
| 98 | +myArray.shift(); // shift() - Removes 1st element from the array |
| 99 | +console.log(myArray); |
| 100 | +myArray.unshift(66); // unshift() - Adds an element to the beginning of the array |
| 101 | +console.log(myArray); |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
0 commit comments