Q:

meaning of tilde in javascript

//Combined with ~, it can do a boolean check if an item exists in a string value

var foo = "hello world";

if (~foo.indexOf("w")) {
  // item in list
} else {
  // item not in list
}
1
//Bitwise operator
console.log(~-2); // 1
console.log(~-1); // 0

//Converting Strings to Numbers
console.log(~~"-1");  // -1
console.log(~~"0");   // 0
1

New to Communities?

Join the community