Hi, I'm Yukti! π
Lately, Iβve been deep-diving into JavaScript through tutorials, projects, and brain-melting practice.
And while doing that, I had this β¨lightbulb momentβ¨:
If you master Strings (and Objects), youβve already got 80% of JavaScript in your pocket.
Strings are everywhere. Like seriously β everywhere.
- Buttons? Strings.
- API responses? Strings.
- Form inputs? You guessed it: Strings.
So I decided, why not write a chill, beginner-friendly guide β not boring, but actually fun and useful. A mix of my notes + tips + βwhy-did-no-one-tell-me-thisβ kind of stuff.
Letβs gooo! πββοΈπ¨
π§΅ 1. What Even Is a String?
In JavaScript, a string is just a bunch of characters wrapped in quotes.
const name = "Yukti"; // double quotes
const hobby = 'Coding'; // single quotes
const mood = `Happy β¨`; // backticks (aka template literals)
Now, backticks (`) are extra cool β you can plug variables directly into them:
Hello, Iβm ${name} and I love ${hobby}
js
const greeting =;
β This was introduced in ES6 and makes life easier.
Bonus Check:
js
console.log(typeof "hello"); // "string"
Yep. Itβs a string, Sherlock. π΅οΈββοΈ
π§ͺ 2. Primitive String vs Object String (aka "Donβt Do This")
Most of us do this (which is good):
js
const name = "Yukti"; // primitive string β
But JavaScript also allows this (which is... π€¨):
js
const strObj = new String("Yukti"); // object string β
These look the same but behave differently:
js
typeof name // "string"
typeof strObj // "object"
Just donβt overcomplicate it. Use primitive strings. Stay chill. π
π οΈ 3. String Methods (The Real Magic Begins)
Letβs talk about the string methods youβll actually use β in projects, problems, and even interviews.
πΉ .length
β How Long Is It?
js
const name = "Yukti";
console.log(name.length); // 5
Spaces count too. Sadly, JS doesnβt ignore your emotional baggage.
πΉ .toUpperCase()
/ .toLowerCase()
`js
const city = "Delhi";
console.log(city.toUpperCase()); // "DELHI"
console.log(city.toLowerCase()); // "delhi"
`
π Great for standardizing user input (like emails).
πΉ .includes()
β Is It In There?
js
const sentence = "I love JavaScript";
console.log(sentence.includes("Java")); // true
JS: βDo you love me?β
You: βLet me .includes() check.β
πΉ .startsWith()
/ .endsWith()
`js
const file = "resume.pdf";
console.log(file.startsWith("res")); // true
console.log(file.endsWith(".pdf")); // true
`
Great for checking file types or filtering URLs.
πΉ .slice(start, end)
β Cut it like cake π
`js
const lang = "JavaScript";
console.log(lang.slice(0, 4)); // "Java"
console.log(lang.slice(-3)); // "ipt"
`
β
Works with negatives
β
Doesnβt change the original string
πΉ .substring(start, end)
β .slice()βs Sibling
`js
const text = "JavaScript";
console.log(text.substring(0, 4)); // "Java"
console.log(text.substring(4, 0)); // "Java" (auto-swaps)
console.log(text.substring(-3, 4)); // "Java" (negative = 0)
`
π Doesnβt support negatives
π Swaps automatically if start > end
π§ Slice vs Substring β Quick Recap
Feature | .slice() | .substring() |
---|---|---|
Negatives | β Yes | β No |
Auto-swap | β No | β Yes |
Use When? | Control | Safety |
π§ Trick: slice = smart, substring = safe
πΉ .replace()
/ .replaceAll()
`js
const msg = "JS is fun. JS is powerful.";
console.log(msg.replace("JS", "JavaScript")); // Only first
console.log(msg.replaceAll("JS", "JavaScript")); // All of them
`
Perfect for cleaning up texts. Or replacing βexβ with βnextβ. π
πΉ .split()
β Break it into pieces
js
const sentence = "I love coding";
console.log(sentence.split(" ")); // ["I", "love", "coding"]
πΉ .join()
β Stitch it back together
js
const words = ["I", "love", "coding"];
console.log(words.join("-")); // "I-love-coding"
πΉ .trim()
β Remove Extra Spaces
`js
const messy = " hello world ";
console.log(messy.trim()); // "hello world"
console.log(messy.trimStart()); // "hello world "
console.log(messy.trimEnd()); // " hello world"
`
β Great for cleaning up copy-pasted input.
πΉ .charAt(index)
vs string[index]
`js
const word = "code";
console.log(word.charAt(0)); // "c"
console.log(word[1]); // "o"
`
Both work β use whichever you vibe with.
π§© 4. String Logic Time (For Interviews or Impressing Your Code Crush)
π Reverse a string
`js
function reverse(str) {
return str.split("").reverse().join("");
}
console.log(reverse("hello")); // "olleh"
`
π Check for Palindrome
`js
function isPalindrome(str) {
const rev = str.split("").reverse().join("");
return str === rev;
}
console.log(isPalindrome("madam")); // true
`
π’ Count Character Frequency
`js
function countFrequency(str) {
const map = {};
for (let char of str) {
map[char] = (map[char] || 0) + 1;
}
return map;
}
console.log(countFrequency("apple"));
// { a: 1, p: 2, l: 1, e: 1 }
`
β οΈ 5. Common Mistakes (JS Strings Being Dramatic)
β Strings are Immutable
`js
let str = "hello";
str[0] = "H";
console.log(str); // still "hello"
`
JS: βYou thought you could change me?β Nope. Try again. π
β‘ Type Coercion Confusion
js
console.log("5" + 1); // "51"
console.log("5" - 1); // 4
JavaScript sometimes acts too smart for its own good. (And confuses beginners in the process.)
π§ 6. Practice Time (Go Try These!)
Try solving these on your own:
β
Capitalize the first letter of every word
β
Find the longest word in a sentence
β
Count vowels
β
Remove duplicate letters from a string
(Donβt worry β Iβm working on a solution set too π)
π Summary
Strings are literally everywhere in your code.
We covered the most useful methods with humor and heart.
Next up: JavaScript Objects β Letβs unlock real power.
π€ Letβs Connect!
Iβm learning out loud and loving it.
Follow me on Dev.to for more code stories, breakdowns, and bite-sized dev wisdom.
Letβs grow together π§ π»
#javascript #webdev #beginners #frontend #codingwithfun #devlife
Top comments (0)