Skip to content

Commit 7e23682

Browse files
Strings and Their Properties
1 parent 76ba4b0 commit 7e23682

File tree

1 file changed

+72
-47
lines changed
  • 05_arrays_and_objects/04_strings_and_their_properties

1 file changed

+72
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,116 @@
1-
# Strings and Their Properties
1+
# 📜 Strings and Their Properties in JavaScript
22

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.
54

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.
727

828
### 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.
1029

11-
#### Example
1230
```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
1634
```
17-
As shown, trying to add a property like `age` to a string value does not work because strings are immutable.
1835

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
2043

21-
### Length Property
2244
The `length` property returns the number of characters in a string.
2345

24-
#### Example
2546
```javascript
26-
let string = "abc";
27-
console.log(string.length); //3
47+
let word = "JavaScript";
48+
console.log(word.length); //10
2849
```
2950

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.
3254

33-
#### Example
3455
```javascript
35-
console.log(string[1]); //b
56+
console.log(word[4]); //S
3657
```
3758

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.
4062

41-
#### Example
4263
```javascript
43-
console.log("coconuts".slice(4, 7)); // → nut
64+
let fruit = "coconuts";
65+
console.log(fruit.slice(4, 7)); // → nut
4466
```
4567

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.
4871

49-
#### Example
5072
```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
5375
```
5476

55-
### trim Method
77+
### 🧹 trim Method
78+
5679
The `trim` method removes whitespace from both ends of a string.
5780

58-
#### Example
5981
```javascript
60-
console.log(" okay \n ".trim()); // → okay
82+
let phrase = " Hello World! ";
83+
console.log(phrase.trim()); // → "Hello World!"
6184
```
6285

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.
6589

66-
#### Example
6790
```javascript
68-
console.log(String(6).padStart(3, "0")); //006
91+
console.log(String(42).padStart(5, "0")); //"00042"
6992
```
7093

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.
7398

74-
#### Example
7599
```javascript
76-
let sentence = "Secretarybirds specialize in stomping";
100+
let sentence = "Hello, world! JavaScript is fun.";
77101
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."
80104
```
81105

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.
84109

85-
#### Example
86110
```javascript
87-
console.log("LA".repeat(3)); //LALALA
111+
console.log("Hey!".repeat(3)); //"Hey!Hey!Hey!"
88112
```
89113

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

Comments
 (0)