Skip to content

Instantly share code, notes, and snippets.

@dillonforrest
Last active December 24, 2015 16:29
Show Gist options
  • Select an option

  • Save dillonforrest/6827931 to your computer and use it in GitHub Desktop.

Select an option

Save dillonforrest/6827931 to your computer and use it in GitHub Desktop.

Revisions

  1. dillonforrest revised this gist Oct 5, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion js_tilde.md
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,6 @@ function contains(str, key) {
    var filtered = _.filter(coll, contains);
    ```

    The cool thing is the `~`. Apparently, when the '~' operator is used with integers other than `-1`, it evaluates to `false`. Otherwise, if the tilde is used with `-1`, it evaluates to `true`. MAGIC.
    ~~The cool thing is the `~`. Apparently, when the '~' operator is used with integers other than `-1`, it evaluates to `false`. Otherwise, if the tilde is used with `-1`, it evaluates to `true`. MAGIC.~~

    EDIT: I misspoke. The `~` will evaluate `-1` to `0`, which is falsy, and will evaluate all other integers to something other than 0, which all will be truthy. :)
  2. dillonforrest created this gist Oct 4, 2013.
    14 changes: 14 additions & 0 deletions js_tilde.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    I just came across this interesting bit of javascript, which I've paraphrased:

    ```javascript
    var _ = require('underscore');
    var coll = require('./some/external/array');

    function contains(str, key) {
    return ~str.indexOf(key);
    }

    var filtered = _.filter(coll, contains);
    ```

    The cool thing is the `~`. Apparently, when the '~' operator is used with integers other than `-1`, it evaluates to `false`. Otherwise, if the tilde is used with `-1`, it evaluates to `true`. MAGIC.