-4

The code below always responds with "hi". Even if i set the campaign1 variable to 0. Can anyone advise if I am writing this out incorrectly?

campaign1 = 2;

campaign_string = (typeof campaign1 > 1 ? "hello" : "hi" );

Kind regards,

Amar
  • 11
  • 3
  • 1
    Why are you using `typeof` to fetch the *value* of a variable? – Frédéric Hamidi Jul 03 '15 at 13:53
  • 3
    Well `typeof campaign1` is always `"number"` regardless of which numerical value you give it. – JJJ Jul 03 '15 at 13:53
  • In my code later on I am concatenating a string with the variable campaign_string. Is there a better way to do this? As i can't use an if statement in my string concat. – Amar Jul 03 '15 at 13:55

3 Answers3

1

You don't need typeof operator. Try repeating without that; and get rid of the unnecessary paranthesis too.

This should do

campaign1 = 2;

campaign_string = campaign1 > 1 ? "hello" : "hi";
1

You are using typeof but not comparing it to a type. I think you mean to do this.

campaign1 = 2;

campaign_string = (campaign1 > 1 ? "hello" : "hi" );

typeof is used like this.

var variable = 'Test';

typeof variable === 'string' // This is true.
Enijar
  • 6,387
  • 9
  • 44
  • 73
0

You dont need this 'typeof', so change it to:

campaign1 = 2;

campaign_string = campaign1 > 1 ? "hello" : "hi" );
Fabjan
  • 13,506
  • 4
  • 25
  • 52