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!

3 posts were split to a new topic: Is JavaScript case sensitive?

A post was split to a new topic: Why did the string disappear?

A post was split to a new topic: Whats wrong with my code?

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

1 Like

Thank you for explaining! :smile:

13 posts were merged into an existing topic: What is the difference between a property and a method, and when do I need to use parenthesis?

2 posts were split to a new topic: Why didn’t .matchAll work?

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

1 Like

7 posts were split to a new topic: What does prototype in the documentation mean?

Hey, sorry if there’s already an answer to my question, but it’s still not clear to me what is main difference between JS object and instance?
Thank you

Technically, since everything in JS is an object, they are all instances of some constructor class or another.

42  =>  instance of Number class

""  =>  instance of String class

[]  =>  instance of Array class

We don’t often refer to common objects as instances. It is the term more used to describe objects we declare with our own custom class.

class Person {
    constructor (name, age) {
        this.name = name;
        this,age = age;

weegillis = new Person('Wee Gillis', 19)

Above, weegillis is an instance of our Person class. We can accept that it is also an instance of the Object class, and treat that as moot.


2 posts were split to a new topic: What is the difference between a property and a method, and when do I need to use parenthesis?

I have absolutely no idea what this is saying can anyone please help me understand this tutorial? How did i get here?

console.log(‘hello’.toUpperCase()); // Prints ‘HELLO’
console.log(‘Hey’.startsWith(‘H’)); // Prints true
Let’s look at each of the lines above:

On the first line, the .toUpperCase() method (?) is called on the string (?) instance (?) ‘hello’. The result is logged (?) to the console (?) . This method returns a string in all capital letters: ‘HELLO’.

On the second line, the .startsWith() method (?) is called on the string instance (?) ‘Hey’. This method also accepts the character ‘H’ as an input, or argument, between the parentheses. (?) (?) (?) (?) (?) Since the string ‘Hey’ does start with the letter ‘H’, the method returns the boolean true. (?) (?) (?) (?) (?) (?)

Can someone please explain what this means?

In the second console.log() statement in app.js , we have a string ' Remove whitespace ' which has spaces before and after the words 'Remove whitespace' .

If we browse the JavaScript string documentation, we find several built-in string methods that each accomplish a different goal. The one method that seems ideal for us is .trim() .

Use the method to remove the whitespace at the beginning and end of the string in the second console.log() statement.

1 Like

Are the parentheses following a method require if nothing is being included within them? Why?

Ex: .toUpperCase()

Yes, even when there is no argument. The () (parens) tell JS to invoke the method on the object in context.

"string value".toUpperCase()  =>  "STRING VALUE"
    object        method   ^
  in context               |

The method is akin to a function, but it is not freestanding. It is inherited from the constructor prototype for String objects and can only be called on that data type.

1 Like

I’m trying to use the trimStart() method from JS documentation but I keep getting an error. Please let me know whether I am doing something incorrectly or this is a bug. Thanks!

console.log(’ Remove whitespace '.trimStart());

(function (exports, require, module, __filename, __dirname) { console.log('    Remove whitespace   '.trimStart());

TypeError: "    Remove whitespace   ".trimStart is not a function
    at Object.<anonymous> (/home/ccuser/workspace/introduction-to-javascript-built-in-methods/app.js:1:102)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

Try using the .trim() method as suggested. It might be the case that the version of Node.js behind the LE is older than Version 10.0 which is the first version to support .trimStart().

1 Like