|
1 |
| -# Strings and Their Properties |
| 1 | +# 📜 Strings and Their Properties in JavaScript |
2 | 2 |
|
3 |
| -## Introduction |
4 |
| -Strings in JavaScript are primitive data types but have several built-in properties and methods that allow for manipulation and inspection. Despite being primitive, they behave similarly to objects in some ways, allowing the use of properties and methods. |
| 3 | +JavaScript strings are primitive data types with a suite of built-in properties and methods that make working with text convenient and powerful. Even though strings are immutable, meaning they cannot be changed after they are created, JavaScript provides numerous ways to manipulate and analyze them. |
5 | 4 |
|
6 |
| -## Immutable Nature of Strings |
| 5 | +## 📖 Table of Contents |
| 6 | + |
| 7 | +1. [🔍 Introduction to Strings](#-introduction-to-strings) |
| 8 | +2. [🔒 Immutable Nature of Strings](#-immutable-nature-of-strings) |
| 9 | +3. [🛠️ Built-in String Methods](#️-built-in-string-methods) |
| 10 | + - [Length Property](#-length-property) |
| 11 | + - [Accessing Characters](#-accessing-characters) |
| 12 | + - [slice Method](#-slice-method) |
| 13 | + - [indexOf Method](#-indexof-method) |
| 14 | + - [trim Method](#-trim-method) |
| 15 | + - [padStart Method](#-padstart-method) |
| 16 | + - [split and join Methods](#-split-and-join-methods) |
| 17 | + - [repeat Method](#-repeat-method) |
| 18 | +4. [💡 Conclusion](#-conclusion) |
| 19 | + |
| 20 | +## 🔍 Introduction to Strings |
| 21 | + |
| 22 | +Strings in JavaScript are text-based data types represented within quotes (`" "` or `' '`). Though they’re **primitive** values, JavaScript treats strings with some object-like behaviors, allowing us to use properties and methods for efficient manipulation. |
| 23 | + |
| 24 | +## 🔒 Immutable Nature of Strings |
| 25 | + |
| 26 | +Strings are **immutable**. This means that once a string is created, its value cannot be changed. Attempts to modify a string directly or add properties won’t work. |
7 | 27 |
|
8 | 28 | ### Example: Attempting to Add Properties
|
9 |
| -Values of type string, number, and Boolean are immutable and not objects. Although you can attempt to set properties on them, those properties won't be stored. |
10 | 29 |
|
11 |
| -#### Example |
12 | 30 | ```javascript
|
13 |
| -let kim = "Kim"; |
14 |
| -kim.age = 88; |
15 |
| -console.log(kim.age); // → undefined |
| 31 | +let name = "Kim"; |
| 32 | +name.age = 88; // Trying to add a property |
| 33 | +console.log(name.age); // → undefined |
16 | 34 | ```
|
17 |
| -As shown, trying to add a property like `age` to a string value does not work because strings are immutable. |
18 | 35 |
|
19 |
| -## Built-in String Methods |
| 36 | +In this example, the `age` property doesn’t persist because strings don’t store properties. |
| 37 | + |
| 38 | +## 🛠️ Built-in String Methods |
| 39 | + |
| 40 | +Despite their immutability, JavaScript strings come with a variety of methods for examining and transforming text. |
| 41 | + |
| 42 | +### 📏 Length Property |
20 | 43 |
|
21 |
| -### Length Property |
22 | 44 | The `length` property returns the number of characters in a string.
|
23 | 45 |
|
24 |
| -#### Example |
25 | 46 | ```javascript
|
26 |
| -let string = "abc"; |
27 |
| -console.log(string.length); // → 3 |
| 47 | +let word = "JavaScript"; |
| 48 | +console.log(word.length); // → 10 |
28 | 49 | ```
|
29 | 50 |
|
30 |
| -### Accessing Characters |
31 |
| -You can access individual characters in a string using bracket notation, similar to arrays. |
| 51 | +### 🔠 Accessing Characters |
| 52 | + |
| 53 | +You can access specific characters in a string using **bracket notation**, similar to arrays. |
32 | 54 |
|
33 |
| -#### Example |
34 | 55 | ```javascript
|
35 |
| -console.log(string[1]); // → b |
| 56 | +console.log(word[4]); // → S |
36 | 57 | ```
|
37 | 58 |
|
38 |
| -### slice Method |
39 |
| -The `slice` method extracts a section of a string and returns it as a new string. |
| 59 | +### ✂️ slice Method |
| 60 | + |
| 61 | +The `slice` method extracts a part of a string and returns it as a new string. |
40 | 62 |
|
41 |
| -#### Example |
42 | 63 | ```javascript
|
43 |
| -console.log("coconuts".slice(4, 7)); // → nut |
| 64 | +let fruit = "coconuts"; |
| 65 | +console.log(fruit.slice(4, 7)); // → nut |
44 | 66 | ```
|
45 | 67 |
|
46 |
| -### indexOf Method |
47 |
| -The `indexOf` method returns the index within the calling string of the first occurrence of the specified value. |
| 68 | +### 🔍 indexOf Method |
| 69 | + |
| 70 | +`indexOf` returns the index of the first occurrence of a specified substring. |
48 | 71 |
|
49 |
| -#### Example |
50 | 72 | ```javascript
|
51 |
| -console.log("coconut".indexOf("u")); // → 5 |
52 |
| -console.log("one two three".indexOf("ee")); // → 11 |
| 73 | +console.log("hello world".indexOf("world")); // → 6 |
| 74 | +console.log("JavaScript".indexOf("Script")); // → 4 |
53 | 75 | ```
|
54 | 76 |
|
55 |
| -### trim Method |
| 77 | +### 🧹 trim Method |
| 78 | + |
56 | 79 | The `trim` method removes whitespace from both ends of a string.
|
57 | 80 |
|
58 |
| -#### Example |
59 | 81 | ```javascript
|
60 |
| -console.log(" okay \n ".trim()); // → okay |
| 82 | +let phrase = " Hello World! "; |
| 83 | +console.log(phrase.trim()); // → "Hello World!" |
61 | 84 | ```
|
62 | 85 |
|
63 |
| -### padStart Method |
64 |
| -The `padStart` method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. |
| 86 | +### 🧩 padStart Method |
| 87 | + |
| 88 | +`padStart` pads the beginning of a string to a specified length with a specified character. |
65 | 89 |
|
66 |
| -#### Example |
67 | 90 | ```javascript
|
68 |
| -console.log(String(6).padStart(3, "0")); // → 006 |
| 91 | +console.log(String(42).padStart(5, "0")); // → "00042" |
69 | 92 | ```
|
70 | 93 |
|
71 |
| -### split and join Methods |
72 |
| -The `split` method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array. The `join` method joins all elements of an array into a string. |
| 94 | +### ✂️ split and 🔗 join Methods |
| 95 | + |
| 96 | +- `split` divides a string into an array of substrings based on a specified delimiter. |
| 97 | +- `join` concatenates all elements of an array into a string with a specified separator. |
73 | 98 |
|
74 |
| -#### Example |
75 | 99 | ```javascript
|
76 |
| -let sentence = "Secretarybirds specialize in stomping"; |
| 100 | +let sentence = "Hello, world! JavaScript is fun."; |
77 | 101 | let words = sentence.split(" ");
|
78 |
| -console.log(words); // → ["Secretarybirds", "specialize", "in", "stomping"] |
79 |
| -console.log(words.join(". ")); // → Secretarybirds. specialize. in. stomping |
| 102 | +console.log(words); // → ["Hello,", "world!", "JavaScript", "is", "fun."] |
| 103 | +console.log(words.join(" | ")); // → "Hello, | world! | JavaScript | is | fun." |
80 | 104 | ```
|
81 | 105 |
|
82 |
| -### repeat Method |
83 |
| -The `repeat` method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together. |
| 106 | +### 🔁 repeat Method |
| 107 | + |
| 108 | +The `repeat` method returns a new string with a specified number of repetitions of the original string. |
84 | 109 |
|
85 |
| -#### Example |
86 | 110 | ```javascript
|
87 |
| -console.log("LA".repeat(3)); // → LALALA |
| 111 | +console.log("Hey!".repeat(3)); // → "Hey!Hey!Hey!" |
88 | 112 | ```
|
89 | 113 |
|
90 |
| -## Conclusion |
91 |
| -Strings in JavaScript, while immutable and primitive, come with a robust set of properties and methods that facilitate various string operations. These built-in functionalities make string manipulation straightforward and efficient. |
| 114 | +## 💡 Conclusion |
| 115 | + |
| 116 | +JavaScript strings, although immutable, are equipped with many built-in properties and methods. These methods allow for flexible and efficient manipulation, making it easy to work with text. Knowing how to use these string methods is essential for effective JavaScript programming. 🚀 |
0 commit comments