FAQ: Introduction to JavaScript - Properties

This community-built FAQ covers the “Properties” exercise from the lesson "Introduction to JavaScript ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Properties

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

7 posts were split to a new topic: Code won’t let me pass

11 posts were split to a new topic: Can’t continue without pressing solution button

4 posts were split to a new topic: Why would the length of a sentence be useful to know?

2 posts were split to a new topic: Difference between a property and a method?

hi in this chapter the characters are supposed to be 25 or 30 ?

console.log("Teaching the world how to code".length);

Is that the line you are measuring?

It’s 30 characters because .length counts each space as a character.

1 Like
m = "23.556"
d = m.substring(m.indexOf('.') + 1).length
1 Like

3 posts were split to a new topic: Is there something wrong with my code?

Hello, what would the .length for numbers be, I tried assigning a variable to a number and then putting the variable in the console.log but it returns undefined, thanks fairly new to this.

Numbers are exact values that don’t have a length. Sure they have numeral characters, etc., but as objects they have no length. Further to this, numbers are not iterable.

1 Like

5 posts were split to a new topic: What does it mean by an instance of a data type?

Given .length counts all characters:

console.log(‘Teaching the world how to code’.length);
results in 30
console.log(‘Teachingtheworldhowtocode’.length);
results in 25

Is there a quick modification to this to count non-space characters, or perhaps only spaces?

We might hope there is, but the truth is we need to devise an algorithm or special method for this purpose. At this early stage it may be too soon to explore this question.

Take for instance the String method, .split(). This method will take any string as a separator, then splits its string on that separator and returns an array of strings.

const words = 'Teaching the world how to code'.split(' ')

In the above, the separator string is a space character. The returned array is,

[ 'Teaching', 'the', 'world', 'how', 'to', 'code' ]

Notice the spaces are now removed. The separator is dropped, and only the characters on either side are retained. Note how many words are in the array. Six, which means there were five spaces in the original string.

Anyway, as stated this may be too soon to expose and explore such methods, so don’t go down this path, just yet. It will surface, shortly.

1 Like

Hi! Why, in every exercise it says //Print 5? What is it for?
For example: console.log(‘Hello’.length); // Prints 5

I understand that its a comment, but is it really necesary put it in the exercises?
Thanks!

It is the same as many examples on the web…

// should print 5
console.log('Hello".length)
1 Like

I didnt get it, sorry :frowning:

Hi everyone! I have a question. Is it the necessary to use ‘hello’.toUpperCase (). to put something in capital letters or can I just put “HELLO”? Thanks!

What we write directly in our code is literally up to us, pun intended. There is no need to call upon the string method to convert case since we’re typing it in, anyway. Where it will matter is when we are working with random program generated data or user inputted data. Our program may have to conform to some standard or desire of the author. If the need is to have all user input in uppercase, then we don’t trust the user and include the hard coded instruction,

user_input.toUpperCase()
1 Like