2018-09-30 23:01:58 +01:00
---
id: bd7123c9c448eddfaeb5bdef
title: Find the Length of a String
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cvmqEAd'
2019-07-31 11:32:23 -07:00
forumTopicId: 18182
2021-01-13 03:31:00 +01:00
dashedName: find-the-length-of-a-string
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You can find the length of a `String` value by writing `.length` after the string variable or string literal.
2021-03-02 16:12:12 -08:00
```js
console.log("Alan Peter".length);
```
2022-04-09 06:27:50 -04:00
The value `10` would be displayed in the console. Note that the space character between "Alan" and "Peter" is also counted.
2020-11-27 19:02:05 +01:00
2021-10-26 01:55:58 +09:00
For example, if we created a variable `const firstName = "Ada"` , we could find out how long the string `Ada` is by using the `firstName.length` property.
2020-11-27 19:02:05 +01:00
# --instructions--
2022-04-15 10:13:42 -05:00
Use the `.length` property to set `lastNameLength` to the number of characters in `lastName` .
2020-11-27 19:02:05 +01:00
# --hints--
You should not change the variable declarations in the `// Setup` section.
```js
assert(
2021-10-26 01:55:58 +09:00
code.match(/let lastNameLength = 0;/) & &
code.match(/const lastName = "Lovelace";/)
2020-11-27 19:02:05 +01:00
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`lastNameLength` should be equal to eight.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof lastNameLength !== 'undefined' & & lastNameLength === 8);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
You should be getting the length of `lastName` by using `.length` like this: `lastName.length` .
```js
assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
// Setup
2021-10-26 01:55:58 +09:00
let lastNameLength = 0;
const lastName = "Lovelace";
2018-09-30 23:01:58 +01:00
2020-03-02 23:18:30 -08:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
lastNameLength = lastName;
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
let lastNameLength = 0;
const lastName = "Lovelace";
2018-09-30 23:01:58 +01:00
lastNameLength = lastName.length;
```