Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all info on a Javascript variable

I'm not a JavaScript Wizard by a long shot. But I am a web-developer and so I need to know my way around it at least a bit.

Something I'll often do is simply alert a variable to see what it is.

However, the problem is that I'll often get a result like: "object HTMLInputElement". To me this means little to nothing. Sure I can look it up, but I need to alert children() of children() of children(), etc...

I've tried walking through the JavaScript with Firebug, but for some reason this is very slow. Firefox hangs when I start a debug session, for every single debug session and I don't know why.

So, I want to inform if there is a way to get detailed info on variables some other way. Or a system I can use to work with to make things easier.

like image 774
KdgDev Avatar asked Mar 14 '11 10:03

KdgDev


People also ask

How do I inspect a JavaScript variable in Chrome?

We can then check what values are stored in any variable at that point of time. Click F12 to open Developer Tools in Chrome. Or we can right-click and select Inspect (Ctrl+Shift+I).

How will you get the information about all variables that are currently in scope?

You can see scopes and their variables in [[Scopes]] , even closure scopes using console. dir() . It shows you the variables in "[[Scopes]] > Closure", "[[Scopes]] > Global" and even "[[Scopes]] > Script" if the page have scripts reachable. Even with nested closures you can see the nested scopes.

How do I print the contents of a variable in JavaScript?

The console.log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.log(" ");


1 Answers

I find the developer tools in Chrome work quite well, giving a good amount of detail on demand (usually just hovering the mouse over the variable in the script tab; if that variable is a structured object, a little tree control appears and you can drill down). But then, I don't have your Firebug issue either (or at least, not often anymore).

Debugging with alert is very time-wasteful and, as you've found, frustrating; if at all possible I'd look at using a real debugger (like Chrome's Dev Tools; I've also heard good things about Opera's).

like image 158
T.J. Crowder Avatar answered Oct 13 '22 00:10

T.J. Crowder