📖 Scenario: Imagine you are working with text data from different countries. You want to understand how strings behave in JavaScript compared to other languages. This project will help you see how strings are stored and manipulated in JavaScript.
🎯 Goal: You will create a string variable, check its length, access characters by index, and print the results to see how JavaScript handles strings.
📋 What You'll Learn
Create a string variable with exact text 'Hello, World!'
Create a variable to store the length of the string
Access the first and last characters of the string using index
Print the length, first character, and last character
💡 Why This Matters
🌍 Real World
Working with text data is common in apps like chat, social media, and data processing. Understanding how strings work helps you handle user input and display text correctly.
💼 Career
Many programming jobs require manipulating strings for tasks like formatting, searching, and validating text data.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called greeting and set it to the exact text 'Hello, World!'
DSA Javascript
Hint
Use const greeting = 'Hello, World!'; to create the string variable.
2
Get the length of the string
Create a variable called length and set it to the length of the string greeting using greeting.length
DSA Javascript
Hint
Use const length = greeting.length; to get the string length.
3
Access first and last characters
Create two variables called firstChar and lastChar. Set firstChar to the first character of greeting using greeting[0]. Set lastChar to the last character using greeting[length - 1]
DSA Javascript
Hint
Use greeting[0] for the first character and greeting[length - 1] for the last character.
4
Print the results
Print the variables length, firstChar, and lastChar each on a new line using console.log
DSA Javascript
Hint
Use console.log(length);, console.log(firstChar);, and console.log(lastChar); to print the values.
Practice
(1/5)
1. In JavaScript, which of the following statements about strings is true?
easy
A. Strings are immutable, meaning you cannot change a character directly.
B. Strings are mutable, so you can change characters by index.
C. Strings are stored as arrays of characters that can be modified.
D. Strings are objects that allow direct character assignment.
Solution
Step 1: Understand string mutability in JavaScript
JavaScript strings cannot be changed after creation; they are immutable.
Step 2: Compare with other options
Options A, B, and C suggest mutability or direct modification, which is incorrect.
Final Answer:
Strings are immutable, meaning you cannot change a character directly. -> Option A
Quick Check:
JavaScript strings are immutable = D [OK]
Hint: Remember: JavaScript strings cannot be changed character by character [OK]
Common Mistakes:
Thinking strings behave like arrays and can be changed by index
Confusing string immutability with array mutability
Assuming strings are objects with mutable properties
2. Which of the following is the correct way to get the length of a string str in JavaScript?
easy
A. str.size
B. str.length()
C. length(str)
D. str.length
Solution
Step 1: Recall JavaScript string length syntax
In JavaScript, string length is accessed as a property without parentheses: str.length
Step 2: Check other options for syntax errors
str.length() uses parentheses which is incorrect; C and D are not valid JavaScript syntax for string length.
Final Answer:
str.length -> Option D
Quick Check:
Use property .length without () = A [OK]
Hint: Use .length property without parentheses for string length [OK]
Common Mistakes:
Using parentheses like a function call (str.length())
Confusing with other languages that use length() function
Trying to use str.size which is invalid in JavaScript
3. What will be the output of the following JavaScript code?
const s = 'hello';
console.log(s[1]);
s[1] = 'a';
console.log(s[1]);
medium
A. e\ne
B. e\na
C. undefined\na
D. undefined\ne
Solution
Step 1: Check first console.log(s[1])
Index 1 of 'hello' is 'e', so first output is 'e'.
Step 2: Attempt to change s[1] and check second console.log(s[1])
Strings are immutable, so s[1] = 'a' does nothing; second output remains 'e'.
Final Answer:
e\ne -> Option A
Quick Check:
Immutable strings keep original chars = B [OK]
Hint: Remember strings can't be changed by index assignment [OK]
Common Mistakes:
Expecting s[1] to change to 'a'
Confusing string mutability with arrays
Assuming assignment to string index works
4. Identify the error in the following JavaScript code snippet:
let str = 'world';
str[0] = 'W';
console.log(str);
medium
A. No error; output is 'World'
B. No error; output is 'world'
C. TypeError because strings are immutable
D. SyntaxError due to invalid assignment
Solution
Step 1: Understand string immutability and assignment
Assigning to str[0] does not change the string; no error is thrown.
Step 2: Check console.log output
Since assignment fails silently, output remains original string 'world'.
Final Answer:
No error; output is 'world' -> Option B
Quick Check:
Immutable strings ignore index assignment = C [OK]
Hint: Index assignment on strings fails silently, no error thrown [OK]
Common Mistakes:
Expecting a TypeError on index assignment
Thinking string changes after index assignment
Confusing with languages that allow mutable strings
5. You want to replace the first character of a string str with an uppercase letter in JavaScript. Which approach correctly achieves this?
hard
A. str.replace(0, str[0].toUpperCase());
B. str[0] = str[0].toUpperCase();
C. str = str[0].toUpperCase() + str.slice(1);
D. str.toUpperCase()[0] + str.slice(1);
Solution
Step 1: Understand string immutability and modification
Direct assignment to str[0] is invalid; strings are immutable.
Step 2: Check each option for correct string replacement
str = str[0].toUpperCase() + str.slice(1); creates a new string by uppercasing first char and adding rest; this works correctly.
Step 3: Analyze other options
B tries direct assignment (invalid), C uses replace incorrectly (replace needs substring, not index), D uppercases whole string then concatenates first char with rest but doesn't reassign to str (str unchanged).
Final Answer:
str = str[0].toUpperCase() + str.slice(1); -> Option C
Quick Check:
Use new string with uppercase first char + rest = A [OK]
Hint: Build new string; can't change characters directly [OK]
Common Mistakes:
Trying to assign directly to str[0]
Using replace with index instead of substring
Uppercasing whole string instead of first char only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.