Javascript · Strings
Length
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Introducing the .length Property
Strings in JavaScript have a special built-in property called
.length
that tells you how many characters are in the string.
To use it, you write the string (or variable containing a string) followed by a dot
.
and then
length
:
let codemon_name = "Zap";
console.log(codemon_name.length);
let message = "Hello";
console.log(message.length);
Notice that
.length
does NOT have parentheses
()
after it. That's because it's a property, not a method (function).
Exercise 2 — Using .length in Practice
The
.length
property counts every character in the string, including spaces, numbers, and special characters!
Try running this code and see what each string's length is:
let name1 = "Pyrochu";
console.log("Length of Pyrochu:", name1.length);
let name2 = "Aqua chu";
console.log("Length of 'Aqua chu':", name2.length);
let code = "Player123";
console.log("Length of Player123:", code.length);
let empty = "";
console.log("Length of empty string:", empty.length);
You can also use
.length
directly on string literals (strings in quotes) without storing them in a variable first!
console.log("Florachu".length);
console.log("Hello World!".length);
Exercise 3 — What Does .length Return?
What will the following code output?
let codemon_type = "Water";
console.log(codemon_type.length);
Options
• 5
• 4
• "Water"
• 6
Hint : The ```.length``` property returns a number representing how many characters are in the string. Count each letter in "Water".
Exercise 4 — Accessing the .length Property
Complete the code to display the length of the Codémon's name:
let codemon_name = "Aquachu";
console.log(codemon_name________);
Options: . length () , size
Hint : Remember: properties are accessed with a dot ```.``` and ```.length``` doesn't need parentheses.
Exercise 5 — Counting Spaces
What is the length of the following string?
let trainer_message = "Go Pyrochu!";
console.log(trainer_message.length);
Hint : Don't forget: spaces are characters too! Count every character including the space between words.
Exercise 6 — Checking String Length
A common use of
.length
is checking how long a string is. Complete this code that displays the length of a username:
let username = "CodemonMaster";
let username_length = ____.____;
console.log("Your username has", username_length, "characters");
Options: username length size count user
Hint : You need to access the ```.length``` property of the string variable. Use the variable name, then dot, then the property name.
Exercise 7 — Codémon Name Validator
Time to practice! Create a simple Codémon name validator.
Your task:
- Create a variable called
codemon_name
with any Codémon name you like
- Create another variable called
name_length
that stores the length of the name
- Display both the name and its length using
console.log()
Example output:
"Florachu has 8 characters"
Starter code
// Create your Codémon name validator here
Back to the course