Skip to content

Instantly share code, notes, and snippets.

@cheeaun
Created September 13, 2010 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheeaun/576711 to your computer and use it in GitHub Desktop.
Save cheeaun/576711 to your computer and use it in GitHub Desktop.
Javascript variable to return 'A' but 'B' when converted to string.
// Answer for http://twitter.com/DmitryBaranovsk/status/24339436227
// Question: #jsquiz var a = …; Define a, so those alerts will work like that: alert(a + ""); // alerts "A"; alert(a); // alerts "B";
var a={toString:function(){return 'B'},valueOf:function(){return 'A'}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment