Chapter 9JavaScript Tutorial~1 min read
Strings आणि String Methods
length, slice, replace, toUpperCase आणि बरेच काही
Strings म्हणजे text data. Messages, names, URLs, HTML content — सगळे strings आहेत. JavaScript मध्ये strings quotes मध्ये लिहितात — single, double, किंवा backticks.
Strings Create करणे
javascript
let greeting = "नमस्कार World"; // Double quotes
let name = 'Rahul'; // Single quotes
let message = `Welcome, ${name}!`; // Template literal (backticks)String Properties आणि Methods
length — Characters count
javascript
let str = "Hello World";
console.log(str.length); // 11 (spaces पण count होतात)
let name = "मराठी";
console.log(name.length); // 5String Methods — Common ones
javascript
let text = "Hello World";
// toUpperCase / toLowerCase
console.log(text.toUpperCase()); // "HELLO WORLD"
console.log(text.toLowerCase()); // "hello world"
// indexOf — position शोधणे (-1 जर नाही)
console.log(text.indexOf("World")); // 6
console.log(text.indexOf("xyz")); // -1
// slice — portion काढणे
console.log(text.slice(6)); // "World"
console.log(text.slice(0, 5)); // "Hello"
// replace — text बदलणे
console.log(text.replace("World", "Marathi")); // "Hello Marathi"
// trim — start/end spaces काढणे
let padded = " Rahul ";
console.log(padded.trim()); // "Rahul"
// split — array मध्ये convert करणे
let csv = "Pune,Mumbai,Nagpur";
console.log(csv.split(",")); // ["Pune", "Mumbai", "Nagpur"]
// includes — text आत आहे का?
console.log(text.includes("World")); // true
console.log(text.includes("xyz")); // falseTemplate Literals — Modern String
Template Literals (Backticks)
javascript
let name = "Rahul";
let marks = 85;
// Old way — messy concatenation
let msg1 = "नमस्कार " + name + "! तुम्ही " + marks + " marks मिळवले.";
// Modern way — Template literal
let msg2 = `नमस्कार ${name}! तुम्ही ${marks} marks मिळवले.`;
console.log(msg2); // नमस्कार Rahul! तुम्ही 85 marks मिळवले.
// Expression inside ${}
console.log(`Pass/Fail: ${marks >= 40 ? "Pass ✅" : "Fail ❌"}`);
// Multi-line string
let html = `
<div>
<h1>${name}</h1>
<p>Marks: ${marks}</p>
</div>
`;✅ Key Points — लक्षात ठेवा
- ▸String = quotes मध्ये text — single, double, backticks
- ▸.length — character count
- ▸.toUpperCase() / .toLowerCase() — case change
- ▸.slice(start, end) — portion extract
- ▸.replace(old, new) — text बदलणे
- ▸.split(separator) — array मध्ये convert
- ▸Template literals: `Hello ${name}` — embed variables
0/13 chapters पूर्ण