Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats does ~~ mean in javascript? [duplicate]

Tags:

javascript

I've been reading through some code and noticed a function:

    randint: function(n) {
        return ~~(Math.random() * n)
    }

I know ~ means "not" but how does ~~ change this return statement?

like image 715
Orane Avatar asked Sep 22 '16 19:09

Orane


People also ask

What does ~~ mean in JavaScript?

The Complete Full-Stack JavaScript Course! The “double tilde” (~~) operator is a double NOT Bitwise operator. Use it as a substitute for Math.

What does double tilde do in JavaScript?

It is used as an alternative for calculating the integer part of a fractional number instead of using Math. trunc(), but the condition is that the given number should be negative.

What does => mean in JS?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What does not mean in JavaScript?

The logical NOT ( ! ) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with boolean (logical) values. When used with non-Boolean values, it returns false if its single operand can be converted to true ; otherwise, returns true .


1 Answers

"but how does ~~ change this return statement?"

Answer: It cuts all fractional digits.

~~42.453754 -> 42

like image 105
tmuecksch Avatar answered Sep 23 '22 01:09

tmuecksch