0

I am having a bit of trouble with some javascript, I have searched for a question with an answer to this, but since it is probably one of the easiest things to do, I doubt anyone but myself could get it wrong.

I'm trying to simply make a content switcher, but the if statement that moved onto the next piece of content does not seem to be working:

//Work out next box
if (i == 2){var x=1;}else{var x=i+1;}

I've created a quick js fiddle (http://jsfiddle.net/TuMEa/5/) to try and get this working, I would be glad if someone could help me, thank you :)

(I have very little JS knowledge, but some programming knowledge (so I understand what things do))

7
  • 4
    You forgot to post a link to JSFiddle. Commented Apr 16, 2014 at 11:05
  • 3
    are u aware that x is only useful inside brackets {}? Commented Apr 16, 2014 at 11:06
  • 2
    Why not var x = i == 2 ? 1 : i + 1;? Also variable scope... Please see SO: JavaScript Variable Scope Commented Apr 16, 2014 at 11:06
  • Probs just an example, methodology is the question not optimisation Commented Apr 16, 2014 at 11:07
  • 1
    JavaScript uses function scoped variables, so the x should be accessible from outside the brackets. Commented Apr 16, 2014 at 11:07

2 Answers 2

1

When you use a variable there are 2 parts to it:

  • declaration
  • definition

Also, {} creates a scope.

A variable lives (can be used) in the scope where it was declared.

Declaration

 var x;

A declaration means telling the program that inside this scope there is a variable called x.

Definition

 x = 3;

A definition means telling the program what value that variable holds ( points to ) and it can only be done if the variable was defined first.

As you can see here, you have to declare your variable outside of the accolades, in a common scope for both the if and the else.

http://jsfiddle.net/2hwG7/

var x, i = 1;
if (i == 2)
    x=1;
else
    x=i+1;

console.log(x);

The ?: operator

Also, as other users mentioned, in javascript there is a ternary operator(conditional operator).

Usage:

condition ? action1 : action2;

The above translates to:

if(condition) action1; else action2;

It is called ternary operator because it is using 3 operands.

Sign up to request clarification or add additional context in comments.

4 Comments

Why not simple var x = i == 2 ? 1 : i + 1;?
Because the user must learn. And it is easier for him to learn this way.
It would be nice if you add about condition operator also.
Thanks Andrei, helped me, and thanks for making it really descriptive, marked as answer
0
i = 0;
x = 0;


if (i == 2){x=1;}else{x=i+1;}

console.log(x);

http://jsfiddle.net/9TXJ9/

You are using var which creats the variable in the block scope only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.