Where can I find some "good" in-depth javascript tutorials?

ibex333

Diamond Member
Mar 26, 2005
4,094
123
106
I am having trouble finding good materials on JavaScript. Websites like w3schools are too "shallow". They only touch upon the most basic uses of statements without ever going into any serious examples or complex uses.

For example, I see plenty of explanations about if/else statements, but I dont see any info about nesting if/else and how to properly nest. Same for loops.


I picked up a book on JavaScript but again, it merely goes into anything remotely useful.

Right now I am having immense difficulty in my college class because my professor wants us to nest if/else statements within loops and nest some loops within that, and without proper understanding of nesting, this is quite a nightmare.

Can someone help me with this stuff?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,696
4,658
75
You're having trouble with nesting? That's unusual. I'm not quite sure how to make nesting more clear to you.

First, you know how an if statement works, right? If the condition is true, the code follows one path. If not, and there's an else statement, the code follows that other path. Now suppose that along one path or the other, another if statement is encountered. Again, the code follows one path or the other, then returns to the path it was on before. For the nested if, "the path it was on before" is inside the first if.

Maybe it would help to imagine that each possible code path is a function?
Code:
if(x) {
  if(y) {
    c();
  } else {
    d();
  }
} else {
  b(y)
}

function b(y) {...}
function c() {...}
function d() {...}
is equivalent to
Code:
if(x) a(y);
else b(y);

function a(y) {
  if(y) c();
  else d();
}

function b(y) {...}
function c() {...}
function d() {...}

If you're having difficulty matching braces, you might need a better text editor. Most people seem to like Notepad++. I personally like Gvim. Its key-commands take some learning and getting used to, but one, "%", will move your cursor back and forth between matching braces, brackets, parentheses, or whatever. "=" will auto-indent a line so that matched braces are at the same indent level. Most good text editors do auto-indenting somehow; I'm not sure about matching.
 

piasabird

Lifer
Feb 6, 2002
17,168
60
91
It really helps if you write the code so everything is indented properly.

What I would suggest is you code some things to see what the code does. Testing the code is part of programming. It helps you to get a hang of it. Nesting if,then,else can be a pain. Just looking at things on paper just leads to guess work. A lot of people are not good at looking at code and telling what it is doing. Wait till they start throwing other verbs and conditions in like OR?

This type of code is not very object oriented. I have seen worse in COBOL. This is kind of like a fall-through method, or spaghetti code.
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
Code:
if (conda) {
    ...
} else {
    ...
}
... can be other conditional blocks.
Code:
if (conda) {
    if (condb) {
        if (condc) {
            ...
        } else if (condd) {
            ...
        } else {
            ...
        }
    } else {
        ...
    }
} else {
    ....
}
Or, maybe:
Code:
function b (condc) {
    if (condc) {
        ...
    } else if (condd) { // condd is global
        ...
    } else {
        ...
    }
}

function a (condb) {
    if (condb) {
        b (condc) // condc is global
    } else {
        ...
    }
}

if (conda) {
    a (condb) // condb is global
} else {
    ....
}
All the same pretty much goes for loops, though replacing loops with functions, when the language directly supports loops, is probably the wrong thing to do.

However out there this sounds, do yourself a favor and learn Scheme. You are having some very fundamental issues, based on this thread and the last. In neither thread have you had problems with anything specifically Javascript-related, though it has been a vehicle. Either use online resources (example), or the book (link--notice cheaper used copies), find an interpreter, and spend some spare time learning it. I guarantee you that as you do so, you see the Javascript you are learning through new eyes (you will also understand why people make and use more complicated languages, after about your millionth closing parenthesis :)).

Since it looks you're just beginning to learn programming in general, even better. Nesting, conditional blocks, loops, nested loops, function returns (the thread from last month), and so on, are basic features/problems of most practical programming languages. While not nearly at the level of Java, C#, or C++, Javascript is a fairly complicated and feature-packed language, and that in itself could very well be getting in your way. LISP, though Scheme especially, is about as far from that as you can get (one could describe the whole language on an index card...with a jumbo sharpie!).

Also, to get your actual question, most in-depth Javascript articles will be over your head, because the stuff you are looking for is generally considered basic programming knowledge. So, introductions will generally assume you know nothing, and try not to get too complicated, or they'll assume you know it already, and get on with the more intricate work. I'm sure there are some in between out there, but it's probably not worth the time to try to find them.

There are a lot of other things you could do, but I'll be willing to bet that trying to learn a LISP will lead you to several epiphanies within the first few days about things you've been having trouble understanding trying to learn in Javascript (such as function/expression composition and decomposition, which is a common theme between both threads you've made thus far). Starting off learning Javascript would not be unlike going from basic arithmetic to linear algebra, without the scalar algebra in between.
 
Last edited:

ibex333

Diamond Member
Mar 26, 2005
4,094
123
106
Code:

if (conda) {
...
} else {
...
}

... can be other conditional blocks.
Code:

if (conda) {
if (condb) {
if (condc) {
...
} else if (condd) {
...
} else {
...
}
} else {
...
}
} else {
....
}

Or, maybe:
Code:

function b (condc) {
if (condc) {
...
} else if (condd) { // condd is global
...
} else {
...
}
}

function a (condb) {
if (condb) {
b (condc) // condc is global
} else {
...
}
}

if (conda) {
a (condb) // condb is global
} else {
....
}

All the same pretty much goes for loops, though replacing loops with functions, when the language directly supports loops, is probably the wrong thing to do.

However out there this sounds, do yourself a favor and learn Scheme. You are having some very fundamental issues, based on this thread and the last. In neither thread have you had problems with anything specifically Javascript-related, though it has been a vehicle. Either use online resources (example), or the book (link--notice cheaper used copies), find an interpreter, and spend some spare time learning it. I guarantee you that as you do so, you see the Javascript you are learning through new eyes (you will also understand why people make and use more complicated languages, after about your millionth closing parenthesis ).

Since it looks you're just beginning to learn programming in general, even better. Nesting, conditional blocks, loops, nested loops, function returns (the thread from last month), and so on, are basic features/problems of most practical programming languages. While not nearly at the level of Java, C#, or C++, Javascript is a fairly complicated and feature-packed language, and that in itself could very well be getting in your way. LISP, though Scheme especially, is about as far from that as you can get (one could describe the whole language on an index card...with a jumbo sharpie!).

Also, to get your actual question, most in-depth Javascript articles will be over your head, because the stuff you are looking for is generally considered basic programming knowledge. So, introductions will generally assume you know nothing, and try not to get too complicated, or they'll assume you know it already, and get on with the more intricate work. I'm sure there are some in between out there, but it's probably not worth the time to try to find them.

There are a lot of other things you could do, but I'll be willing to bet that trying to learn a LISP will lead you to several epiphanies within the first few days about things you've been having trouble understanding trying to learn in Javascript (such as function/expression composition and decomposition, which is a common theme between both threads you've made thus far). Starting off learning Javascript would not be unlike going from basic arithmetic to linear algebra, without the scalar algebra in between.

Thanks, but do you really think it's a good idea for me to start learning LISP when I have to hand in a very difficult Javascript assignment on Tuesday involving everything I do not understand right now?
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
Thanks, but do you really think it's a good idea for me to start learning LISP when I have to hand in a very difficult Javascript assignment on Tuesday involving everything I do not understand right now?
Not that soon, no (get the cram work done, first). But maybe Wednesday :). The thing about LISPs is that I've known quite a few non-programmers who were introduced with them (CLISP or Scheme), as part of non-programming STEM programs, and all them picked it right up, despite no prior programming experience, and no real programming interest. Meanwhile, even with programming experience, the first time working with such a language is mind-blowing.
 

trexpesto

Golden Member
Jun 3, 2004
1,237
0
0
Go baby-steps: write it out in English, transform it to "pseudocode" (generally on separate lines), and then code it.

Javascript has a nice "for..in" structure where you can say:
var items = [1,2,3];
for(item in items){
console.log("Hut " + item);
}


Here is some command line copy-paste from node.js, which gives you an interactive way to work with the javascript interpreter (the ">" aka "prompt" is the program's way of showing you it is ready to accept keyboard input), and do little tests to learn basic syntax without editing and saving files:

> var items = {name:"ted", age:1, veg:"spinach"};
undefined
> console.log(items);
{ name: 'ted', age: 1, veg: 'spinach' }
undefined
> for(item in items){console.log(item);}
name
age
veg
undefined
>

(Note: It says undefined because node expects return value from everything.)

I found python interpreter command line to be similarly very useful for initial language intro and figuring out the quirks of command structures, but I had plenty of java already so that is different.

Good luck!
 

Robert Munch

Senior member
Oct 11, 2006
899
0
76
Have you tried codeacademy.com ? Got a nice head start learning javascript and practicing from the exercises.
 
Sep 29, 2004
18,656
68
91
Nesting just means having a block of code inside of an outer block.

Computer Science A set of data contained sequentially within another.

Quite honestly, w3 schools has everything you need to know. When it comes to programming, you need to learn things by trial and error. Take what people said and try to get your stuff working. The process of doing it on your own will force you to learn more along the way. Pretty much why people often say "stop asking us to do your homework"
 

Dratickon

Junior Member
May 13, 2012
21
0
0
Go baby-steps: write it out in English, transform it to "pseudocode" (generally on separate lines), and then code it.

Javascript has a nice "for..in" structure where you can say:
var items = [1,2,3];
for(item in items){
console.log("Hut " + item);
}

Be careful with this. The 'item' iterator in your example refers to the array index, so for it to print "Hut 1 Hut 2 Hut 3" the code would have to be:
Code:
var items = [1,2,3];
for(item in items){
  console.log("Hut  " + items[item]);
}

Javascript (with jQuery) is one of my favorite languages and it's very widely used, so you can pretty much type any question into Google and find an answer to the problem. My advice is to keep at it and ask questions until stuff starts to stick. You'll eventually get to the point where stuff will start to make sense and you'll be able to quickly find or write the tools you need in order to solve any issues you encounter.