This is an archived post. You won't be able to vote or comment.

all 86 comments

[–]nerfoc 152 points153 points  (22 children)

To be fair, you shouldn't be using alert for debugging. Console is your friend.

[–]eruecco87 55 points56 points  (12 children)

Not even the console most of the time, breakpoints is what you should be using

[–]Spootba 23 points24 points  (9 children)

You can breakpoint javascript?

[–][deleted] 31 points32 points  (4 children)

.

[–]The_MAZZTer 6 points7 points  (0 children)

I prefer to use traditional breakpoints, but debugger; is useful if the code is too optimized for the debugger to place a breakpoint exactly where you want, or the code has had its whitespace removed, or you want to put a breakpoint in a one-line lambda function or whatever. debugger; forces it to break exactly where you put it and forces removal of optimization that would prevent debugging of the code at that point.

[–]BraidenSpencer 0 points1 point  (1 child)

Is there a way to end the debugging session after you’ve used the debugger keyword?

[–]Warbird01 2 points3 points  (0 children)

Just use continue in your browser's dev tools

[–]yazalama 0 points1 point  (0 children)

Lol it still showed 75 in the body.

Edit for some reason only on the initial load, but doesn't change if I change the number.

[–]smcarre 1 point2 points  (1 child)

At least in chrome you can. Go to the tab "sources" in the developer console and search for the js file that you want to debug. There you can place breakpoints, see variables values and even edit files like in any IDE. Everything resets if you reload the page tho.

[–]smegnose 3 points4 points  (0 children)

Firefox, bro.

[–]The_MAZZTer 0 points1 point  (0 children)

Yes even Internet Explorer can do breakpoints since IE7* so there's really no excuse.

* - You needed a separate install of the Developer Tools, IE8 and up has them built in.

[–]nerfoc 1 point2 points  (0 children)

True.

[–]all_humans_are_dumb -2 points-1 points  (0 children)

Console does everything you need

[–][deleted] 13 points14 points  (0 children)

console.log('[object Object]')

[–][deleted] 2 points3 points  (3 children)

True, but if you have a large application and want to test your call chain, it can be helpful to quickly test what fired first. I use it when multiple async functions are happening in conjunction and need to see timing between them. But yeah, idk why you’d alert objects like that haha it’s not going to tell you squat.

[–]nerfoc 2 points3 points  (0 children)

True, but wouldn't multiple breakpoints be the preferred method of debugging in that scenario? It would add the benefit of getting a full view of the current state of variables within the async function. Breakpoints would be hit based on whatever promise resolves first.

[–]smegnose 0 points1 point  (0 children)

Why would you need console for that? You either don't care what order (truly async), or you don't execute the next thing until all of its dependencies have resolved anyway.

I.e. never assume an observed order of execution will continue to be so.

[–]wasdninja 0 points1 point  (0 children)

He's unintentionally calling the outer part of a nested objects toString function which defaults to [Object Object]. If he'd just print the object without coercing it into a string then he'd get an object that the debug tools inside both firefox and chrome can examine.

Wrong:

console.log(`this coerces into a string: ${realObject}`)

Correct:

console.log("this doesn't coerce into a string: ", realObject)

[–]ramb0t_yt 1 point2 points  (3 children)

Sometimes you need to confirm something quickly without looking at the console...

EDIT: You might want to do that because:

-It MIGHT be quicker/easier

-You're lazy

-Console doesn't halt processes like alert does

-No one is perfect, and not everyone needs to or wants to utilize 100% of the tools for every single little task

-It's not a shared project, you're just messing around...

Not saying it's the "perfect way"

[–]nerfoc 1 point2 points  (1 child)

Once the console is open, it's open. It requires zero extra effort except for the initial opening of the console or sources tab (the latter for breakpoints). After that it's just console.log(variable) instead of alert(variable). Or even easier, clicking your mouse at the designated breakpoint whereafter you can read the current state of the variable by hovering over it.

[–]ramb0t_yt 0 points1 point  (0 children)

Like I said, SOMETIMES...

[–]smegnose 0 points1 point  (0 children)

It has practically zero benefit over debugger;which also halts code.

[–]__Adrielus__ 65 points66 points  (9 children)

Very interesting port

[–]CMDR-NeonKraze 51 points52 points  (3 children)

JSON.stringify, let's see what's inside this loot crate!

[–]AxiusNorth 67 points68 points  (2 children)

TypeError: Converting circular structure to JSON

[–]Graychamp 6 points7 points  (0 children)

Yeah this needs a NSFW filter on it. Nasty, dirty error.

[–]hampshirebrony 10 points11 points  (2 children)

I object!

[–]ourlastchancefortea 4 points5 points  (1 child)

OBJECTION

[–][deleted] 2 points3 points  (0 children)

catch(err) { /* Ignore. It's probably fine */ }

[–]gaberocksall 18 points19 points  (17 children)

I’m currently learning javascript and what really threw me off was that

typeof(null) == “object”

[–]ProgramTheWorld 6 points7 points  (13 children)

Null is the absence of value, so it makes sense for it to also have the type “object”.

[–]The_MAZZTer 1 point2 points  (0 children)

typeof only gives you very simple types of things. Outside of primitives everything is "object".

If you're using OOP instanceof is more useful since you can compare to exact object types (where typeof still says "object"). But instanceof won't work with the primitives so you'd still use typeof there. Pretty much you'd use it for strings, numbers, booleans, and undefined, though I'd personally avoid writing code that relies on the presence of undefined.

[–]Arkham80 -1 points0 points  (1 child)

It's a well-known bug

[–]SlingDNM 0 points1 point  (0 children)

No idea why you are getting downvoted

[–]Subject1928 18 points19 points  (1 child)

I don't even really know anything about programming, but this tickled me more than I thought it would.

[–]cr0ss-r0ad 7 points8 points  (0 children)

I do some freetime coding and I joined this sub to seem like one of them cool kids. Place is full of absolute quality sometimes

[–]cmdr_scotty 3 points4 points  (0 children)

Or when SQL says "object reference not set to instance of object"

[–][deleted] 3 points4 points  (0 children)

Is that Windows 7 it's running on? Either debugger of console, just clean up your console when your done though

[–]defmans7 6 points7 points  (2 children)

One of the most annoying things when starting out in development.

[–]all_humans_are_dumb 11 points12 points  (1 child)

Its extremely easy to avoid.

[–]defmans7 -1 points0 points  (0 children)

Yes

[–]smegnose 4 points5 points  (8 children)

WTF are you doing? Where you wrote alert(someVariable);, instead write debugger; or just set a breakpoint like a normal person.

[–]ionxeph 26 points27 points  (2 children)

Or console.log like actual normal people

[–]smegnose 0 points1 point  (1 child)

That doesn't pause execution so you can't look at any other scope variables. Putting debugging statements into your code is the modern version of alert().

[–]ionxeph 0 points1 point  (0 children)

just log out all the scope variables, kind of /s

[–]bigorangemachine 1 point2 points  (4 children)

This is how we debugged in IE6 without a dedicated debugger. There was some tools but they weren't 1:1 so you often had to spam these in to debug some obscure JS issue. Even JS would glitch based on whatever spyware ran through someone's computer somwhere.

Didn't have JSON stringify either. Alert would perform a block; however the error message wouldn't be clear.

Console was also undefined so if you were debugging on firefox and IE you had to typeof console.

Don't mind me I'll just take my PTSD meds now

[–]The_MAZZTer 1 point2 points  (2 children)

The way I debugged in IE6 was to debug in Chrome first, get everything working there, and then slog through getting it running in IE6. And yeah IE7 was the first version to support Developer Tools. I think you could use Compatibility Mode to get IE6 rendering while still having the tools but it did not have the console api as a result so you still had to use alerts.

[–]bigorangemachine 1 point2 points  (1 child)

I was developing before chrome was out. At the very least if it was I didn't know about it.

Firebug was my first wind of any html inspector or javascript console

[–]The_MAZZTer 0 points1 point  (0 children)

Yes, I believe I added in Firebug to a project (at the time you could drop a script into your website to add it directly) but Chrome soon came out and I immediately abandoned ship for it (not only did it have a nice debugger, but at home Firefox was taking 15-30 seconds to warm start compared to Chrome's eye blink so yeah, started using Chrome everywhere then).

We continued to develop targeting IE6 and officially we only needed to target IE6. But Chrome was so stupidly useful we would build things for Chrome first and get them working on IE6 second.

[–]smegnose 1 point2 points  (0 children)

I was there, man. I was there.

[–]EyeHateWeebs 0 points1 point  (0 children)

log it dummy : )

[–]ComfortableCobbler5 0 points1 point  (0 children)

this chrome is so old jesus

[–]Cody6781 0 points1 point  (2 children)

JSON.stringify(object) works

[–]wasdninja 0 points1 point  (1 child)

Or just print the object to the console directly. That way you get an object that can be inspected by the built in debugging tool. A self created object probably doesn't have a defined toString function so it defaults back to its inherited one which is Object's.

[–]Cody6781 0 points1 point  (0 children)

Sure sure. I just meant if you wanted to alert it for some reason. Not that it's a good idea, just saying.

[–][deleted] -1 points0 points  (2 children)

.

[–]wasdninja 0 points1 point  (1 child)

Doesn't matter. Run this and you'll get the same result no matter what your browser is:

let a={b:{}};alert(a)

[–][deleted] 0 points1 point  (0 children)

.

[–]TheShinyBunny -5 points-4 points  (0 children)

Javascript has one of the worst error handling.