0

Short questioion, I'm trying to understand this tutorial: http://superdit.com/2011/02/09/jquery-memory-game/

Being new to Javascript I can't seem to find what the statement '== ""' means... I understand "==", but not the empty double quotes.

Akshay
  • 3,361
  • 1
  • 21
  • 19
Carl Papworth
  • 1,282
  • 2
  • 16
  • 35
  • 7
    Accidentally hit submit too early? – Rob W Oct 10 '12 at 07:28
  • 2
    `x == ""` means "is x equal* to the empty string?" (note this comparison isn't strict, so if x is the number 0 the expression still evaluates to true) – NullUserException Oct 10 '12 at 07:28
  • 2
    Can you paste the block that has that code? That page contains a wall of code potential responders would not want to read through. – Joseph Oct 10 '12 at 07:29
  • The code on that particular tutorial isn't an example of great JavaScript...What are you trying to do? – elclanrs Oct 10 '12 at 07:31
  • @JosephtheDreamer: Ctrl+F, `== ""` finds one match: `if (imgopened == "")`. The OP should've pasted a snippet here, I agree, but it's not that hard to find what part of the code this is about – Elias Van Ootegem Oct 10 '12 at 07:41

3 Answers3

7

val == "" is a non-strict comparison to emtpy string. It will evaluate to true if val is empty, 0, false or [] (empty array):

var val = "";
console.log( val == "" ); // true

val = 0;
console.log( val == "" ); // true

val = false;
console.log( val == "" ); // true

val = [];
console.log( val == "" ); // true

You can use === to use strict comparison, fex:

val = 0;
console.log( val === "" ); // false
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
1

The ' == "" ' is a check for an empty string. It will be true when the string is empty, and false whenever there are some characters inside it.

John Sterling
  • 1,091
  • 7
  • 13
0

A quick scan of the code (ctrl-F is your friend) quickly teaches you that the only time such a statement occurs in the code is here: if (imgopened == ""), another search taught me that imgopened is an evil (global) variable that is initialized to "" at the very top of the script, and every time some action/function is done with whatever value it was assigned.

I suspect it's a sort of card game, where two identical imgs need to be clicked, in which case this var will reference the image currently turned. If it's empty, then all imgs are facing down, and this var is empty: "".
In other words:

if (imgopened == "")//=== if no card is turned
{
    //do X, most likely: turn card
}
else
{
    //do Y
}

This could've been written as

if (!imgopened)
//or
if (imgopened == false)//falsy, but somewhat confusing
//or
if (imgopened == 0)//!confusing, don't use
//or, my personal favorite
if (imgopened === '')
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149