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!

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 jQuery Basics (2014) Creating a Password Confirmation Form Perform: Part 1

Alexei Parphyonov
Alexei Parphyonov
34,128 Points

Arrow Function Syntax doesn't seem to be working

Hei. I wanted to write passwordEvent according to Arrow Function Syntax, but it doesn't seem to be working, while it works with traditional function declaration. I get the following mistake

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    at n.fn.init.val (jquery-1.11.0.min.js:4)
    at HTMLInputElement.passwordEvent (app.js:15)
    at HTMLInputElement.dispatch (jquery-1.11.0.min.js:3)
    at HTMLInputElement.r.handle (jquery-1.11.0.min.js:3)

The function itself looks like

const passwordEvent = () => {
  if ($(this).val().length < 9) {
  } else {

Earlier I noticed that I failed to declare anonymous functions in events in jQuery using arrow syntax either. Are they not compatible?

And here it's opposite, I declare an arrow function outside of jQuery, but it fails to use it.

2 Answers

It will never work with arrow functions. The value of this inside an arrow function is determined by the surrounding scope. Since the function is in the global scope, this will refer to the window object. Now you are trying to get the value of the window, which is undefined and jQuery internally tries to call the method toLowerCase on undefined, which causes an error, because undefined has no methods.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,074 Points

The arrow syntax only works on the EcmaScript 6 version of javascript, you will need both Node Js and Babel in order to utilize this syntax in your project, which is way outside the scope of the course you are in.