If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Newbie with some Javascript questions

Hi everyone, I am taking a Javascript course for college. My teacher is horrible, but I need to take the class this semester, and he is the only option. I have some questions.
1. I use Notepad++ to type the code. I have noticed that sometimes it doesn't highlight certain keywords, and this will cause errors in the browser debug console (I primarily use Chrome). For instance, on this line:
var currentTime= calendarDay.getTime();
var is usually turned blue and italicized. It doesn't always happen, and I have to delete it and retype it (just the word var).
2. I remeber seeing somewhere that I should not use the Tab key, only spaces. Is this true?
3. I have tried IE, Chrome, Safari, and Firefox (with the Firebug extension). Is there anyway to get a more descriptive reason for an error. For example, it might say:
Uncaught ReferenceError: yearly is not defined
when the problem is a mispelled variable name somewhere in the yearly function.
I also have an issue with some particular code, but in the interest of brvity, I will start a new thread. Thanks in advance!

1. if the syntax is correct, it shouldn’t matter whether it is highlighted or not. remember JS is case-sensitive.

2. it is more like a coding convention. both variants have pros and cons. (e.g. the width of a Tab is always depending of the settings of the displaying application)

3. the problem with error messages is that they only tell you what error occurred, not why the error occured, because the machine cannot guess your intention of the code. (compare it to a criminal case, you have a dead body with a shot wound (the error) but you have to find out whether it was murder or an accident (the error cause))

The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.

Users who have thanked Dormilich for this post:

1. It seems like if it is not highlighted, Firefox and IE will give me an error until I fix it. Chrome doesn't seem to have an issue. Is there a better text editor I should try?

2. Got it. Cool, thanks.

3. I understand that. However, I had it give me an error message for a function, when the error was actually that I forgot the closing curly bracket for the following function that the "error" function called (if this makes sense). I guess what I need is a tutorial to show how to use the debugger. Anyone recommend one?

I use notepad++ and have never seen the keyword var not get highlighted. As per Dormlich's suggestion, I would suspect that that there is a spelling issue there. Your code will not (for the most part) guess what you are trying to do, it will just fail. So you should be careful.

You can see how your curly brackets are paired in notepad++ by clicking on one of them - they will both turn red, which is a good way of keeping track of if you are doing what you actually mean to do. The same goes for parenthesis and square brackets, btw.

There are lots of debugging tutorials out there - just google "javascript error console tutorial" along with the name of the browser (each works in a different way, so it's worth checking them all out). Obviously the tutorials that come from the manufacturer (that would be Mozilla in the case of firefox) are probably a good place to start.

Users who have thanked xelawho for this post:

I don't know what was going on with the var thing.I know I didn't misspell it. Maybe a glitch? No big deal, it is easy enough to see if it isn't highlighted.

I learned that "matching bracket turns red" thing after my mistake, but thank you for letting me know.

I thought about Googling tutorials as I wrote that. It has been a long morning.

Thank you for your tips XelaWho. I appreciate it.

I have posted heavily on other forums (mainly car forums), but this forum surprised me. I can't believe how quickly people answered my questions, and in such a nice, helpful manner. I sincerely want to thank everyone for being so cool!

1. I use Notepad++ to type the code. I have noticed that sometimes it doesn't highlight certain keywords, and this will cause errors in the browser debug console (I primarily use Chrome). For instance, on this line:
var currentTime= calendarDay.getTime();
var is usually turned blue and italicized. It doesn't always happen, and I have to delete it and retype it (just the word var).

If it helps, you can disable the italics. When I use Notepad++, I disable them because I don't like seeing parts of my code italicized.

This can be done through Settings: Style Configurator, making sure the value in the "Style" column is "Global override", making sure the "Italic" checkbox is unchecked, and checking the "Enable global italic font style" checkbox. This will override all italicization rules in more specific styles.

Originally Posted by RC102782

2. I remeber seeing somewhere that I should not use the Tab key, only spaces. Is this true?

I strongly prefer tabs to spaces:

If you decide you want tab indent widths of three spaces instead of four (my preference), for example, you can simply change a preference in most applications that will resize every tab in the document.

If someone else is reading your code and pastes it into their editor or vice versa, the tabs will automatically change to the indent width specified in that person's editor; they don't have to manually change them.

Tabs don't require you to mash the space key in deeply indented code. Four spaces versus one tab indent with a width of four spaces requires a quarter of the key presses and is, thus, less likely to discourage one from using proper indentation. While many editors auto-indent, mitigating this, there will be cases where you'll need to manually type out the indents.

Tabs require fewer bytes if the indent width is greater than one. It's a marginal benefit.

If you're coding a project, they may require spaces as part of their style guide though. I think the Mozilla project requires this, for example. The idea is to force all of the programmers to use a consistent coding style.

Originally Posted by RC102782

3. I have tried IE, Chrome, Safari, and Firefox (with the Firebug extension). Is there anyway to get a more descriptive reason for an error. For example, it might say:
Uncaught ReferenceError: yearly is not defined
when the problem is a mispelled variable name somewhere in the yearly function.

I think that message is as descriptive as you'll get.

Originally Posted by RC102782

1. It seems like if it is not highlighted, Firefox and IE will give me an error until I fix it. Chrome doesn't seem to have an issue. Is there a better text editor I should try?

My preferred editor is Sublime Text 2 now after using Notepad++ for years. The code style is much more elegant in most cases (with a few oddities in CSS styling when unrecognized keywords are used). It also has a space/tab conversion tool in the lower right of the status bar.

I still use Adobe Dreamweaver CS6 when I need to upload files to a server though. I often have both editors open.

Originally Posted by RC102782

I guess what I need is a tutorial to show how to use the debugger. Anyone recommend one?

The messages won't always be clear, but they're usually as clear as they can be. Debuggers can only read your code, not your mind. When they're not as clear as they should be, it helps to run multiple browser debuggers on the same code. Sometimes Chrome has a better error message than Firefox, for example.

For every complex problem, there is an answer that is clear, simple, and wrong.