FAQ: Introduction to JavaScript - Methods

This community-built FAQ covers the “Methods” 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 Methods

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!

Do I have to distinguish uppercase and lower case when I use a Javascript methods.

Are they different ’ console.log(‘hello’.toUpperCase()); ’ and ’ console.log(‘hello’.touppercase()); ’ ?

JavaScript is case sensitive so all methods and functions must be written the way they appear in the specs.

.toUpperCase()  versus  .touppercase()

.toLowerCase()  versus  .tolowercase()

.toString()     versus  .tostring()

and so on. We cannot vary from the specified names.

It is typical in JS when combining words in a name to use camelCase, with the first letter in lowercase, and the rest of the word starters capitalized.

Another less typical form is called snake_case but we rarely see that in JS. It’s more of a Python thing.

2 Likes

Conversely, this does not apply to JS operators.

typeof

being a perfect example. Look down this rabbit hole, too.

why did the codeacademy string dissapeared in the first place. and did the ‘remove whitespace’ is an instruction.

3 Likes

ive typed the exact same thing the solution gives me and its still saying its wrong, am i using the wrong . ?

2 Likes

At the second instruction, we are told to use the trim() function to remove the whitespaces from console.log(’ Remove whitespace '); leaving only ‘Remove whitespace’.

Why does .trim() not also remove the space between ‘Remove’ and ‘whitespace’?

built-in methods like .trim() are written by a developer, as such they decide the behavior of the method. For trim it was decided only to remove trailing and leading spaces

Thank you for explaining! :smile:

Not really happy with the way this exercise justifies the use of the () that appends methods such as console.log(), as it pertains to *.length.

If .length is a method(as the exercise claims) then it too should be appended by (). I’ve read elsewhere that .length is not a method but rather a property.

So which is it? A property or a method? And why claim it’s one thing and not the other?

A property of all iterables.

object.length

Object has a length property but it is always 1, if I’m not mistaken.

 > Object.hasOwnProperty('length')
<- true
 > Object.length
<- 1
 >

Iterables include arrays, sets and strings.

https://javascript.info/iterable

1 Like

thanks. that contradicts the information in the exercise, but that helps clarify things

1 Like

What is the main difference between Javascript methods (like toUpperCase() and trim()) and properties (like length in previous exercise)? It seems that they have similar functionality.

Similar notation, but different functionality. All objects have a length property, though some of them are fixed at zero (not iterable). Only strings have a .toLowerCase() and .trim() method.

This breaks down to two levels, the top level, Object and the secondary level, String. .length is an attribute of the Object class, and since that is the top of the prototype chain, all subclasses can reach up the chain to the length attribute.

Attributes that are values have no functionality, but those that are methods do have it. Be sure to reach out to MDN for a greater understanding of String methods, Object.prototype, and other JS concepts.

Hello all! I was working through the list of additional methods and came across some issues when trying to use the .matchAll() method. I typed in console.log(‘Codecademy’.matchAll(‘eca’)); and it gave me an error. Please let me know whether I am doing something incorrectly or this is a bug. Thanks!

What is the error you are getting? Please post your code sample.

Why use this console.log(’ Remove whitespace '.trim()); as opposed to just removing spaces when typing it out.

because you can’t always do that. The string might come from user input (field), or a database. You don’t always hard-code a string

sometimes the lesson wants to teach a concept

I have a question about the docs.
When searching for something like methods available for strings, the documentation shows:

String.prototype

and then in the method area something like:

String.prototype.charAt(index)
– Returns the character (exactly one UTF-16 code unit) at the specified index .

It throws me off because I am not sure where the word prototype comes into the mix. What does that mean? Why not just String Methods? or String.method?

I suppose this has always been my issue when looking at docs is that I get there, and there is extra stuff I don’t understand. More than what is being explained in tutorials.

Objects all have a constructor, or class, which has its own methods, but also methods that it affords to all members, or instances.

Objects do not inherit from Object. To call its methods we must use the Object context. Every constructor/class has a prototype that defines the inherited attributes for each instance. In most cases, these attributes are methods.

JavaScript looks for methods on the prototype chain. Every object has a prototype chain by which to access methods. Everything traces back to the parent object, Object. It is the last objective link in the chain. After that there is only null.

The top of the prototype chain is, null. After that it is Object.prototype. After that we begin to split off new special or custom objects which each have their own prototype.

We inherit the charAt() method from String.prototype. We are however carrying out that method on the string object itself. Since its object type has inherited the method from String.prototype we already have an instance of the method without referring to it along the prototype chain. We can access it directly on the instance (context).

"Abracadabra".charAt(6)    //  d
1 Like