Lesson 2
Strings in JavaScript
Your Name in Quotes
Store your name in a variable called name and print it.
let name = "Maya";
console.log(name);
console.log(name);
Single vs Double Quotes
Write your favorite place using single quotes, then double quotes, and confirm both work.
let first = "Maya";
let last = "Putri";
console.log(first + " " + last);
let last = "Putri";
console.log(first + " " + last);
Sticking Strings Together (+)
Combine your first and last name into a full name, with a space in between.
let first = "Maya";
let last = "Putri";
console.log(first + " " + last);
let last = "Putri";
console.log(first + " " + last);
Template Literals (Backticks)
Rewrite Exercise 3 (full name) using backticks and ${} instead of +.
let name = "Maya";
let age = 10;
console.log(`My name is ${name} and I am ${age} years old.`);
let age = 10;
console.log(`My name is ${name} and I am ${age} years old.`);
String Length
Check the length of their name. Bonus: whose name in the group is the longest?
let word = "JavaScript";
console.log(word.length);
console.log(word.length);
Shouting and Whispering
Introduce .toUpperCase() and .toLowerCase().
Task: Take their name and print a "shouting" version (all caps) and a "whispering" version (all lowercase).
let word = "hello";
console.log(word.toUpperCase());
console.log(word.toLowerCase());
console.log(word.toUpperCase());
console.log(word.toLowerCase());
Slicing a String
Use .slice() on your name to print just the first 3 letters (their "nickname").
let word = "JavaScript";
console.log(word.slice(0, 4));
console.log(word.slice(0, 4));
Build a Name Badge
Fill in your own name, age, and favorite color, then run it to see their personalized badge pop up. Great for a "show and tell" moment at the end.
let name = "Maya";
let age = 10;
let favColor = "blue";
alert(`🪪 NAME BADGE 🪪
Name: ${name.toUpperCase()}
Age: ${age}
Favorite Color: ${favColor}
Nickname: ${name.slice(0, 3)}`);
let age = 10;
let favColor = "blue";
alert(`🪪 NAME BADGE 🪪
Name: ${name.toUpperCase()}
Age: ${age}
Favorite Color: ${favColor}
Nickname: ${name.slice(0, 3)}`);