JavaScript Me String Methods & Properties Kya Hote Hain?
JavaScript me string ek tarah ka data type hota hai jo text represent karta hai. Yeh characters ka sequence hota hai jo single (''
), double (""
), ya backticks (``
) ke andar likha jata hai.
JavaScript mein String Methods & Properties ka use strings ko manipulate aur analyze karne ke liye hota hai. Properties jaise length
string ki length batati hai. Methods jaise toUpperCase()
aur toLowerCase()
case change karte hain. trim()
extra spaces hatata hai, slice(start, end)
ek part extract karta hai, aur replace(old, new)
text replace karta hai. split(separator)
string ko array mein todta hai, aur concat()
strings ko jodta hai. charAt(index)
ek specific character return karta hai. Yeh sare methods JavaScript mein strings ke saath kaam karne ko easy aur efficient banate hain.
Example:
let str1 = "Hello World"; // Double quotes
let str2 = 'JavaScript'; // Single quotes
let str3 = `Hello ${str2}`; // Template literal (backticks)
JavaScript String Properties
String objects ke kuch important properties hote hain jo hum string ke bare me information nikalne ke liye use kar sakte hain.
1. length Property
JavaScript length property ek built-in property hai jo arrays, strings aur objects ki length ya size batata hai. Agar aap kisi array ka .length check karte ho, toh yeh usme kitne elements hain, yeh batata hai. Agar string ka .length dekho, toh total characters count karta hai, including spaces. Yeh zero-based index follow nahi karta, balki total count return karta hai.
let text = "Hello JavaScript";
console.log(text.length); // Output: 17
Explanation: Yahan text
me 17 characters hain (spaces bhi count hote hain).
2. String Immutability
Strings in JavaScript are immutable, meaning they cannot be changed after creation.
let str = "Hello";
str[0] = "Y"; // This will not change the string
console.log(str); // Output: Hello
JavaScript String Methods
String ke saath kaam karne ke liye JavaScript me kai built-in methods hote hain.
1. charAt(index)
charAt(index) ek JavaScript string method hai jo kisi string ke specific index par maujood character ko return karti hai. Yeh method zero-based indexing follow karti hai, matlab pehla character index 0, doosra 1, aur aise hi aage badhta hai. Agar diya gaya index valid nahi hai, toh yeh empty string “” return karega. Example: “Hello”.charAt(1) output dega ‘e’, kyunki “Hello” me index 1 par ‘e’ hai. Yeh method read-only hoti hai, iska matlab string ko modify nahi karti, sirf uss position ka character return karti hai. Yeh string manipulation aur validation ke liye useful hoti hai.
let text = "JavaScript";
console.log(text.charAt(4)); // Output: S
Explanation:
Index 0 se start hota hai, to index 4 par ‘S’ hai.
2. charCodeAt(index)
charCodeAt(index) ek JavaScript method hai jo kisi string ke specific index par maujood character ka Unicode value return karti hai. Yeh method ek integer value deta hai jo us character ka Unicode code point hota hai. Index zero-based hota hai, yani pehla character ke liye index 0 hoga, doosre ke liye 1, aur aage bhi isi tarah. Agar diya gaya index valid nahi hai, toh NaN return hota hai. Yeh method khas tor par tab useful hoti hai jab hume kisi character ka ASCII ya Unicode value nikalni ho, jaise encryption ya text processing tasks me.
let text = "ABC";
console.log(text.charCodeAt(0)); // Output: 65
Explanation:
A ka ASCII code 65 hota hai.
3. concat(str1, str2, …)
concat(str1, str2, …) ek JavaScript string method hai jo multiple strings ko ek saath jodkar ek naya string return karti hai. Iska use tab hota hai jab humein alag-alag strings ko bina kisi separator ke combine karna ho. Ye original strings ko modify nahi karti, balki ek naya string generate karti hai.
let str1 = "Hello";
let str2 = "World";
let result = str1.concat(" ", str2);
console.log(result); // Output: Hello World
Explanation:
concat() multiple strings ko combine kar deta hai.
4. includes(searchValue, startIndex)
includes(searchValue, startIndex) JavaScript string aur array methods ka ek built-in function hai. Yeh check karta hai ki koi specific searchValue diya gaya string ya array ke andar exist karta hai ya nahi. Agar milta hai toh true return karta hai, warna false.
Agar startIndex diya gaya ho, toh search uss index se start hoti hai. Default startIndex 0 hota hai.
let text = "JavaScript is awesome";
console.log(text.includes("awesome")); // Output: true
console.log(text.includes("java")); // Output: false (case-sensitive)
Explanation:
Yeh true ya false return karta hai.
5. indexOf(searchValue, startIndex)
indexOf(searchValue, startIndex) JavaScript ka ek method hai jo kisi string ya array me kisi particular value ka index (position) find karne ke liye use hota hai.
searchValue: Wo value jo aap dhoondhna chahte hain.
startIndex (optional): Yahan se searching start hogi (default 0 hota hai).
Agar searchValue milta hai, to uska first occurrence ka index return hota hai. Agar nahi milta, to -1 return hota hai.
let text = "Hello JavaScript";
console.log(text.indexOf("JavaScript")); // Output: 6
console.log(text.indexOf("World")); // Output: -1 (not found)
Explanation:
Agar word nahi mila to -1 return hota hai.
6. lastIndexOf(searchValue, startIndex)
lastIndexOf(searchValue, startIndex) JavaScript ka ek method hai jo kisi string ya array me diye gaye searchValue ka aakhri occurrence (last position) dhoondta hai. Agar startIndex diya ho, toh search us index tak limit hoti hai. Agar searchValue nahi milta, toh -1 return hota hai.
let text = "JavaScript is JavaScript";
console.log(text.lastIndexOf("JavaScript")); // Output: 15
Explanation:
JavaScript last time index 15 par mila.
7. slice(start, end)
slice(start, end) ek JavaScript method hai jo kisi array ya string ke ek specific portion ko extract karne ke liye use hoti hai. Yeh method original array ya string ko modify nahi karti, balki ek naya portion return karti hai. start index se slicing shuru hoti hai, aur end index se ek position pehle tak chalta hai. Agar end specify nahi kiya jaye, toh slicing last tak hoti hai. Negative indexes se hum end se counting kar sakte hain. Example: arr.slice(1, 4) pehle index se lekar teesre index tak ka portion return karega, lekin chautha index exclude rahega.
let text = "Hello JavaScript";
console.log(text.slice(6, 16)); // Output: JavaScript
Explanation:
Start index include hota hai, end index exclude hota hai.
8. substring(start, end)
substring(start, end) ek JavaScript method hai jo ek string ka ek hissa (substring) extract karne ke liye use hoti hai. Yeh method do parameters leti hai: start aur end. start index se shuru hota hai, aur end index tak extract karta hai, lekin end index include nahi hota. Agar end specify nahi kiya gaya, toh yeh string ke end tak substring le lega. Negative indexes allow nahi hote. Example: “Hello”.substring(1, 4) ka output “ell” hoga, kyunki index 1 se 3 tak extract hota hai. Yeh method immutable hoti hai, yani original string change nahi hoti.
let text = "JavaScript";
console.log(text.substring(0, 4)); // Output: Java
Explanation:
substring(0,4) me index 4 exclude hoga.
9. substr(start, length)
substr(start, length) ek function hai jo ek string ke kisi specific hissa (substring) ko extract karne ke liye use hota hai. Yahan start us position ko represent karta hai jahan se substring shuru hogi, aur length batata hai ki kitne characters extract karne hain. Yeh function JavaScript aur kai doosri programming languages me use hota hai. Agar length specify nahi kiya jaye, to string ke end tak ka data return hota hai. Example: “Hello”.substr(1,3) output dega “ell”, kyunki index 1 se shuru hoke 3 characters extract hote hain. Yeh method dynamic text manipulation ke liye useful hai.
let text = "JavaScript";
console.log(text.substr(4, 6)); // Output: Script
Explanation:
Start index 4 se 6 characters extract honge.
10. replace(searchValue, newValue)
replace(searchValue, newValue) ek JavaScript string method hai jo kisi string ke ek specific part ko doosre value se replace karne ke liye use hoti hai. searchValue wo value hoti hai jise replace karna hai, aur newValue wo value hai jo us jagah aayegi. Ye method sirf pehle matching occurrence ko replace karti hai, agar global replace chahiye to regular expression ka use hota hai. Example: “Hello World”.replace(“World”, “JavaScript”) ka output “Hello JavaScript” hoga. Agar multiple occurrences replace karni ho, to /World/g jaise regex ka istemal hota hai.
let text = "Hello JavaScript";
let newText = text.replace("JavaScript", "World");
console.log(newText); // Output: Hello World
Explanation:
Sirf pehli occurrence replace hoti hai.
11. toUpperCase() & toLowerCase()
toUpperCase() & toLowerCase() JavaScript ke built-in string methods hain jo kisi bhi string ke case (uppercase ya lowercase) ko change karne ke liye use hote hain.
toUpperCase() method ek string ke sabhi lowercase letters ko uppercase me convert karta hai.
toLowerCase() method ek string ke sabhi uppercase letters ko lowercase me convert karta hai.
let text = "Hello JavaScript";
console.log(text.toUpperCase()); // Output: HELLO JAVASCRIPT
console.log(text.toLowerCase()); // Output: hello javascript
12. trim(), trimStart(), trimEnd()
trim()
, trimStart()
, aur trimEnd()
JavaScript ke built-in string methods hain jo whitespace (spaces, tabs, newlines) ko remove karne ke liye use hote hain.
-
trim()
– Yeh string ke dono ends (start aur end) se whitespace hata deta hai. -
trimStart()
– Sirf string ke starting se whitespace remove karta hai. -
trimEnd()
– Sirf string ke end se whitespace remove karta hai.
let text = " Hello JavaScript ";
console.log(text.trim()); // Output: "Hello JavaScript"
console.log(text.trimStart()); // Output: "Hello JavaScript "
console.log(text.trimEnd()); // Output: " Hello JavaScript"
13. split(separator, limit)
JavaScript me split(separator, limit) ek string method hai jo ek string ko todkar ek array me convert karta hai. separator ek optional parameter hai jo specify karta hai ki kis character ya regex ke basis par string split hogi. Agar separator na diya jaye, toh pura string ek single element wale array me return hota hai. limit ek optional parameter hai jo specify karta hai ki kitne maximum splits hone chahiye. Agar limit diya jaye, toh output array sirf utne hi elements tak restrict hota hai. Yeh method original string ko modify nahi karta.
let text = "Apple, Banana, Mango";
let fruits = text.split(", ");
console.log(fruits); // Output: ["Apple", "Banana", "Mango"]
JavaScript me string methods aur properties bahut useful hote hain jo hume text manipulation me madad karte hain. Har method ka specific use case hota hai jo humare code ko efficient aur readable banata hai.
Quiz: Test Your Knowledge on JavaScript String Methods & Properties
Bonus: Practical Application!
Aaj hi apne JavaScript code me String methods & properties ka istemal karke dekhein!
JavaScript String methods & properties ko sahi tareeke se samajhne ke liye different types jaise length
, toUpperCase()
, toLowerCase()
, charAt()
, substring()
, slice()
, replace()
, concat()
, aur trim()
ka upayog karein aur apne code ko aur bhi powerful banayein.