Strings and Template Literals
A string is just text, and JavaScript treats it as a sequence of characters — you can index into it and pull substrings out of it much like an array.
main.js
Try it yourself →let greeting = "Hello, World!"; console.log(greeting[0]); console.log(greeting.slice(7, 12)); console.log(greeting.toUpperCase()); console.log(greeting.length);
Output
H World HELLO, WORLD! 13
- greeting[0] gets the character at index 0 — JavaScript indexes from 0, just like Python
- greeting.slice(7, 12) is a slice — characters from index 7 up to (not including) index 12
- toUpperCase() returns a new string, it doesn't change the original
- length is a property, not a function — no parentheses, unlike Python's len(greeting)
Template literals — the modern way to build text
You'll constantly need to combine variables with fixed text. Template literals (strings wrapped in backticks) let you drop a variable straight into a string using ${...}, instead of clumsily concatenating pieces with +.
main.js
Try it yourself →let name = "Abhimanyu";
let age = 25;
console.log(`My name is ${name} and I am ${age} years old.`);Output
My name is Abhimanyu and I am 25 years old.
Next: arrays — JavaScript's way of storing an ordered collection of values.
Practice exercise
Create text = "Hello, World!". Print just the word "World" using text.slice(7, 12).