tags:

views:

62

answers:

3

New to javascript, I face many problems.

I read the javascript tutorial at w3cschools.com, and there are many question marks on my head.

I do not understand what is the difference below:

var name=something;

name=something;

The above two examples also giving something to a name, why 2 different ways?

name=new Array();

name[0]=something0;

name[1]=something1;

is this same with switch?

//switch start

var name=something();

switch(something)

{

case 1:

do something;

break;

case 2:

do something;

break;

default:

do something;

}

//if...else start

var name=something();

if (condition) 

{

do something

};

else if (condition)

{

do something

};

else

{

do something

};

what is the different between switch case and else.if ?

i think both 2 is doing the same thing?match condition and then do something?

and the for Loops,while Loops and break Loops ,

both 3 are doing the same thing,but 3 different ways.

can someone tell me what is the different between them?it make me confuse.

and please intro more tutorial for javascript.

many thanks here

+4  A: 

For your first question, when u use "var", it defines a local scope to the variable. When you use variables without keywork "var", it means they are global variable. Usually its not a good practice to use global variables.

Also on other IF ELSE parts, you cant have semicolon before ELSE IF block.

Check these links -

Read all articles under JavaScript: http://www.crockford.com/

JavaScript: Scoping and Hoisting: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

JavaScript: Function expressions vs. Function declarations vs. Function statements: http://yura.thinkweb2.com/named-function-expressions/

Sachin Shanbhag
A: 

In answer to your first question:

var name=something;

name=something;

var name

This snippet creates a new variable, called name. It will be refered to in the rest of your code as name, it has been declared.

var name = something

This piece of code assumes that there is a variable declared above it called 'something', and it creates the name variable and assigns it the value of whatever 'soemething' holds at that particular point.

name = something

Without the intitial creation of the variable, this line assumes that the variable has already been declared previously, it is simply assigning that variable the value or something.

Reading

I recommend you read this webpage which appears to have a great introduction to javascript and will answer many more of your questions.

Tom Gullen
nope. Upon instantiation, not using var puts the variable in the global scope instead of the local scope. that's the difference. See Sachin Shanbhag's answer.
pferdefleisch
A: 

and a quick, easy answer to your switch question. switch is just often cleaner and more readable than a repeated else if block.
You can also do cool stuff like regular expressions in your case (case /awesome|radical/: ...). You will appreciate that later ;)

pferdefleisch
Does anybody ever really 'appreciate' regex :)
Paddy
Dude. I don't appreciate them, I love them. And I don't just love them, I'm *in* love with them :P
pferdefleisch