Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Strings Comparing Strings

Comparing Strings

After watching the "Comparing Strings" video I decided to test out some code for learning purposes, but I need help.

In the video at 5:10 / 9:48 Jim went over this first function below:

function compare(a, b) {
    console.log(a + ' < ' + b,  a < b)
}

compare('a', 'b');

This prints in the console: a < b true

Ok easy.

But why do I get: Uncaught SyntaxError: Unexpected Number

When I try this function out?

function comparison(1, 2) {
    console.log(1 + ' < ' + 2, 1 < 2)
}

comparison('1', '2')

Could it be that I am getting the syntax errors because I am using numbers in my argument ( i.e. (1,2) )?

2 Answers

Purvi Agrawal
Purvi Agrawal
7,960 Points

Yes, using numbers in function definition can be the mistake. Generally, we use variables in function definition and numbers or values in function call. Try using variables in function definition i.e. function comparison(a, b) . Also, u can directly pass numbers as comparison(1, 2) instead of comparison('1', '2'). The one u used is converting numbers into string as enclosed in single quotes. Hope it would be helpful.

function comparison(a, b) {
    console.log(a + ' < ' + b, a < b);
}

comparison(1, 2);

I tried this method and it worked. Problem solved, thanks Purvi Agrawal !