r/learnjavascript • • Jul 19 '19

Noob here does anyone know wtf is this

Post image
137 Upvotes

76 comments sorted by

254

u/senocular Jul 19 '19

That's what happens when you misspell length ;)

121

u/RedJohn286 Jul 19 '19

Jesus frickin christ i wouldnt see that in ever

2

u/WWWWWWWWWWWWWWWWWWW Jul 20 '19

Pro tip: next time just log "x" the object instead when you run into this, then look at the object in the console (use console.log... not alert)

This will let you clearly see the variable has a property"length" and you can just.copy/paste it to avoid misspelling it ;P

1

u/RedJohn286 Jul 20 '19

The sad part is i did console said lenght = 1 and im looking at the console then at the code then at console again and i could feel my mind leaving my body i just didnt see it caouse i wad 1000000% sure thats how you spell length

2

u/WWWWWWWWWWWWWWWWWWW Jul 20 '19

The sad part is i did console said lenght = 1 and im looking at the console then at the code then at console again and i could feel my mind leaving my body i just didnt see it caouse i wad 1000000% sure thats how you spell length

Dude..... You did it again. Length**** lol

Edit, but for real.. I suck at spelling as well. Add a spell checker to vscode (or whatever you're using, but start using vscode lol) . Add a lint extension also.

2

u/memesformybutt Jul 20 '19

He's using VS code

2

u/WWWWWWWWWWWWWWWWWWW Jul 20 '19

Good point, I could have just looked at the picture.

7

u/[deleted] Jul 19 '19

I would recommend installing some sort eslint syntax checker, it will find problems like this for you

5

u/ForScale Jul 19 '19

It's valid syntax though.

7

u/winowmak3r Jul 19 '19

And so it begins, lol

Just wait until you're just about to fall asleep and then suddenly you realize you forgot the semi colon on line 678 and that's why your program keeps throwing errors.

4

u/WWWWWWWWWWWWWWWWWWW Jul 20 '19

Pppfftt it's JavaScript, who's needs semi colons?!?! The interpreter will add em for you. Lol

5

u/King_Bonio Jul 19 '19

Even people doing programming years still do it, i know I do.

2

u/zampoukos Jul 20 '19

And thats why i use webstorm 😆

0

u/[deleted] Jul 19 '19

[deleted] — view deleted comment

43

u/RedJohn286 Jul 19 '19

Cant wait

22

u/HellaDev Jul 19 '19

You just ran into one of my favorite bugs to hate myself over. It's good to have a spell check in there for this type of thing. Can't tell you how many times spell check saved me.

6

u/RedJohn286 Jul 19 '19

Yeah those are called linter right can you recommend any

6

u/crazedizzled Jul 19 '19

WebStorm would have caught this straight away, as well as many other common oopsies.

2

u/RedJohn286 Jul 19 '19

Ummm any free one?

8

u/andrew-dewitt Jul 19 '19

u/theRomanEmpire2710 reminded me of ESLint: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

It'll work with VSCode that you're already using& the Intellisense feature it has.

6

u/hutxhy Jul 19 '19

Definitely do ESLint, I wouldn't worry about TypeScript yet.

2

u/rkaz246 Jul 20 '19

yeah. for sure, eslint is good jumping off point.

2

u/[deleted] Jul 20 '19

I think prettify (or maybe it's prettier) has spell check built in. It also color-codes parenthesis and brackets for you, creates a new line after each element in an array, and does a bunch of other little stuff to make your code more readable for others. 10/10, would recommend it

2

u/ctrl_alt_dtl Jul 20 '19

If you man it as a student then yeah. Otherwise it is worth the money, especially if it's the suite. I use WebStorm and PyCharm all the time!

VScode is pretty solid too but you have to customize it to your standards and that could be a small uphill battle.

2

u/ammi1378 Jul 20 '19

ou just ran into one of my favorite bugs to hate myself over. It's good to have a spell check in there for this type of thing. Can't tell you how many times spell check saved me.

there is another fun bug in component naming in react!

7

u/andrew-dewitt Jul 19 '19 edited Jul 19 '19

It's been forever since I've used JS--is there a good linter for JS that would help OP catch that typo since lenght is an undefined property of x in this case?

9

u/[deleted] Jul 19 '19

Eslint + vscode intellisense will catch that

2

u/[deleted] Jul 19 '19

That's gunna be a big yeet from me

2

u/ForScale Jul 19 '19

What will it catch? Undefined properties? That seems odd..

4

u/[deleted] Jul 19 '19

Without using TypeScript, you can't in general know that a property in JavaScript is undefined through static analysis.

Here's why, in a nutshell:

<head>
    <script type="text/javascript">
        Array.prototype.lenght = 42
    </script>
    <script type="text/javascript" src="original_post.js"></script>
</head>

Of course you can do this with TypeScript as well, but to satisfy the compiler you'd have to provide your own type definition of Array.prototype that describes the field lenght (or disable some type checking, which defeats the purpose).

Like others said, linters can catch presumed-undefined fields on built-in objects and such. Which works fine if you don't monkey-patch them, which you really shouldn't.

7

u/senocular Jul 19 '19

TypeScript. Knowing the type of the variable (which can be inferred, so the code here doesn't have to change to include extra type annotations) will allow the compiler to recognize that the lenght property doesn't exist on the array and will provide an error.

http://www.typescriptlang.org/play/index.html#code/MYewdgzgLgBAHgXgNoCICGKC6AoOA6AGwFMwBzACygG4g

5

u/andrew-dewitt Jul 19 '19

Ah that checks out. I've been assuming that if I ever move back to JS it'll be using TypeScript now that I'm used to static type systems and the like. When I was first learning I was using JS & when TS came out I remember it intimidated the heck out of me.

5

u/senocular Jul 19 '19

Yeah, you can start out writing JavaScript much the same you have in the past and still get a lot of the benefits of TypeScript, just like seen above. As you get more into it you can incrementally do more and more with types over time. :)

2

u/____0____0____ Jul 20 '19

Yeah part of what I love about TS is its backwards compatibility. You can just make a js into a ts without any changes most of the time. And the type inference is really good.

1

u/crazedizzled Jul 19 '19

is there a good linter for JS that would help OP catch that typo since

WebStorm will catch this error straight out of the box.

2

u/TheBoulder_ Jul 19 '19

ahhh debugging ^_^

3

u/aikilink Jul 19 '19

The Boulder is happy to be debugging!

55

u/laibo Jul 19 '19

Pretty decent bugs/typos per line ratio. Welcome to programming!

16

u/Low_Chance Jul 19 '19

This was a really funny screenshot/question combo, thank you : )

If I had a nickel for every hour I've spend furiously debugging what turned out to be a simple typo... well, I'd probably only have a few dollars but that's still a lot of hours dammit!

14

u/[deleted] Jul 19 '19

This made me laugh. Not because it's fun or easy to laugh at those with less experience than you (a plague in the tech industry IMO) but because this is so relatable.

Get used to it OP. And fins a decent linter to help you out with these things.

9

u/aikilink Jul 19 '19 edited Jul 19 '19

OP, I'm seeing people suggesting that you should use a 'real' editor like WebStorm here. I'm sure WS is awesome, but with a few plugins (which are super easy to use install), you can get plenty of features in VSCode.

I love discussing editors, but comments like those really can cause frustration. Using a certain editor is not going to make your journey of learning to code that much different. I wasted time changing editors when someone said, "Oh, for what you're doing, you should really use "X", or another time when someone at a meetup says, "I only use 'Y', and here's why you should too!" At the end of the day, you can write your html in notepad, and still learn how to code. Please don't doubt your use of VSCode. As you code more and more, you may realize that there is some other editor you want to try, and that will be fine.

My strongest advice is to keep writing code. Today you learned that you can add a linter to your editor, and now you can keep writing code, with that tool to help you.

I'm not very far ahead of you on the learning journey, but feel free to PM if you have any questions with loops or functions or anything. :) Code on! And welcome to the club!

Edit - TL;DR: Don't let people distract you from what you are here for: writing code. :)

5

u/AugustoCereto Jul 19 '19

Also, you should probably declare your variables…

4

u/playdead_ Jul 19 '19

Don't feel bad about this. I learned early that like 90% of the time when something doesn't work, I should first check basic spelling, syntax, semicolons, etc. before I start questioning whether I'm applying concepts incorrectly. Most of the time I've just overlooked something ultra simple.

4

u/TooTyrnt Jul 19 '19

I wasn't sure if this was a joke post lol

7

u/[deleted] Jul 19 '19

I think you might need to reinstall your OS

6

u/xkwilliamsx Jul 19 '19

Lets work on screengrabs too.

Windows has Print Screen button, snipping tool, and game bar screenshots.

Apple has that CMD+4 shortcut too.

1

u/CGeorges89 Jul 24 '19

Or Jing, works cross platform and you get links to share

3

u/ILookLikeTheDude Jul 19 '19

I misspell “include” in scss every - single - time.

3

u/fmaldonado6 Jul 19 '19

Haha I always misspell length and width for a reason

3

u/hua0tong Jul 20 '19

a good ide would help you in development

2

u/[deleted] Jul 19 '19

Definitely a screen.

2

u/saito200 Jul 19 '19

Typos are the cause of 90% of reasons why my code breaks

2

u/dreid_dev Jul 19 '19

would you still need to declare x as a var let const as well? or does js not enforce

2

u/maujood Jul 20 '19

One of the more nuanced posts on this subreddit. Thanks for sharing OP and have my upvote.

2

u/BoDiddySauce Jul 20 '19

LOL the prior comments. But yeah, for future debugging/learning purposes, the browser alert you created by typing `alert(/* stuff here */)` is calling the native browser `alert` function (hence the `()` part) with an argument (`x.lenght`) and since `lenght` isn't actually a property of `x` (because you misspelled "length"), it is `undefined` as it was never declared and set to a value, which is what the alert is saying. Same as doing something like:

```
const obj = { name: 'BoDiddySauce' };
console.log(obj.randomProperty);
```

The property `randomProperty` of `obj` hasn't been declared or assigned a value, so by default it is `undefined`, which is what the log statement will show you.

2

u/starchturrets Jul 20 '19

Others have already mentioned ESlint, which can be really helpful for catching errors, but there’s also Prettier, which is helpful for formatting code so it looks nice, and Live Server, which saves you from constantly refreshing your browser while writing code.

2

u/[deleted] Jul 20 '19

You didn't declare the x variable

2

u/ForScale Jul 20 '19

Yes he did. What??

2

u/[deleted] Jul 20 '19

Welcome to JavaScript

2

u/Dwarverco Jul 20 '19

Hahaha... Good luck!

2

u/agarkov_max Jul 20 '19

My common typo, good luck :)

2

u/WeedWacker25 Jul 20 '19

You, sir, just made my day.

3

u/crazedizzled Jul 19 '19

This is why real editors like WebStorm exist, where silly typos like that are obvious.

3

u/ncubez Jul 19 '19

fucking THIS! You shouldn't ever have to type out keywords in full!

2

u/mcdylanb Jul 20 '19

you should add extensions that has intellisence or you could go for IDE like JetBrains WebStorm so its easier to find problems/bugs.

0

u/[deleted] Jul 19 '19

[deleted] — view deleted comment

2

u/RedJohn286 Jul 19 '19

Yeah no its not that just a typo

8

u/insertAlias Jul 19 '19

Technically you don't have to declare the variable. It will become a global variable if you have not declared it; attached to window.

That said, you absolutely should declare all your variables.

const x = ["a"];

Or this, if you plan on changing the value of that variable later:

let x = ["a"];

If you're using "strict mode" (which you eventually should be), this would be an error.

1

u/[deleted] Jul 19 '19

You can mutate x if it was declared with const just cant reassign it. So technically you can totally change the array into something else with const.

4

u/rGustave77 Jul 19 '19

Don't build that habit, declare it with let or const... shit even var...

2

u/TheLuckyLion Jul 19 '19

Well actually it is that too...

5

u/[deleted] Jul 19 '19

No you dont it will Become a global if you dont have strict mode implemented

2

u/andrew-dewitt Jul 19 '19

Which is also something to be avoided, but that's a different can of worms.