📖 Scenario: Imagine you are working on a simple text editor that needs to handle and display user input strings correctly. Understanding how strings are stored and manipulated in memory helps you manage text efficiently.
🎯 Goal: You will create a string variable, check its length, access characters by index, and finally print the string and its length to understand how strings work in memory.
📋 What You'll Learn
Create a string variable with exact text
Create a variable to store the string length
Access a specific character in the string by index
Print the string and its length
💡 Why This Matters
🌍 Real World
Text editors, chat apps, and websites all use strings to store and display text. Understanding string basics helps in handling user input and displaying messages.
💼 Career
Many programming jobs require working with strings for data processing, user interfaces, and communication between systems.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called text and set it to the exact value "Hello, World!".
DSA Javascript
Hint
Use const to create a variable and assign the string exactly as shown.
2
Find the length of the string
Create a variable called length and set it to the length of the string text using the .length property.
DSA Javascript
Hint
Use text.length to get the number of characters in the string.
3
Access a character by index
Create a variable called firstChar and set it to the first character of text using index 0.
DSA Javascript
Hint
Use square brackets [] with index 0 to get the first character.
4
Print the string and its length
Print the string text and the length length using two separate console.log statements.
DSA Javascript
Hint
Use console.log(text) and console.log(length) to print the values.
Practice
(1/5)
1. What does the term immutable mean when talking about strings in JavaScript?
easy
A. Strings cannot be changed after they are created
B. Strings can be changed anytime
C. Strings are stored as numbers
D. Strings are always empty
Solution
Step 1: Understand string immutability
In JavaScript, once a string is created, its content cannot be changed directly.
Step 2: Compare options with definition
Strings cannot be changed after they are created - this matches the definition.
Final Answer:
Strings cannot be changed after they are created -> Option A
Quick Check:
Immutable means unchangeable = D [OK]
Hint: Remember: strings can't be changed once made [OK]
Common Mistakes:
Thinking strings can be changed like arrays
Confusing immutability with emptiness
Assuming strings are stored as numbers
2. Which of the following is the correct way to create a string in JavaScript?
easy
A. const str = Hello World;
B. const str = (Hello World);
C. const str = `Hello World;
D. const str = 'Hello World';
Solution
Step 1: Check string syntax rules
Strings must be enclosed in quotes: single (' '), double (" "), or backticks (` `).