What is (~~) "double tilde" operator in JavaScript ? Last Updated : 30 Jul, 2024 Comments Improve Suggest changes Like Article Like Report This is a special kind of operator in JavaScript. To understand the double tilde operator, first, we need to discuss the tilde operator or Bitwise NOT. The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000). So if we think closely it can be noticed that after inverting a number twice it will be the same as before, for example, invert the (011000) again and it would become (100111) as it was earlier.We can reach the number from where we have started after using the double tilde (~~) operator, then what is the use of this overhead? The concept is while inverting the number this (~) operator converts them to a 32-bit signed integer which doesn't keep fractional values. And when we invert that signed integer again, it results in a normal inversion, and due to this, the number either becomes the floor (an integer less than or equal to the number) of the original number or ceil (an integer greater than or equal to the number) of the original number.Below are the usages of this operator.Usage:It is used as an alternative for calculating floor instead of using Math.floor(), but the condition given number should be positive.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.It is used as an alternative for calculating the ceil instead of using Math.ceil(), but the condition that is given the number should be negative.It can be used to convert false values like undefined to zero so it becomes useful when we want to create a counter on such values without facing any errors.Example 1: The following example demonstrates both of the above functions. First of all, we have declared a variable, and the prompt is taking input from the user. We are using the ~~ operator which will calculate the floor of the number, If the entered number was positive, or it calculates ceil of the number if the entered number was negative. JavaScript let x; x = window.prompt("Enter Any positive Fractional Number"); console.log("The number is ", x); if (x > 0) { let floorOfX = (~~x); console.log("The floor of the given number is ", floorOfX); } else { let ceilOfX = (~~x); console.log("The ceil of the given number is ", ceilOfX); } Output:Example 2: In this example, we will create a counter to check the number of duplicates of a particular type in an array using tilde JavaScript let number=[1, 2, 3, 1, 1, 7]; let histogram={}; number.forEach( num => histogram[num] = ~~histogram[num] + 1 ); console.log("histogram[1]==" + histogram[1]); Output:dupCount[1]==3Explanation: We do not get any error of undefined while using the counter as the double tilde operator converts the previously undefined value i.e. the value of the undefined array element to zero and it can be used to increment the counter. Comment More infoAdvertise with us Next Article What is (~~) "double tilde" operator in JavaScript ? mrtwinklesharma Follow Improve Article Tags : JavaScript Web Technologies javascript-operators JavaScript-Questions Similar Reads What is the !! (not not) Operator in JavaScript? The !! (double negation) operator is a repetition of the unary logical "not" (!) operator twice. It is used to determine the truthiness of a value and convert it to a boolean (either true or false). Hereâs how it works:The single ! (logical "not") inverts the truth value of a given expression:!false 2 min read What does +_ operator mean in JavaScript ? Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used t 2 min read What does OR Operator || in a Statement in JavaScript ? JavaScript is a dynamic programming language that allows developers to write complex code with ease. One of the fundamental concepts in JavaScript is the use of operators, which are symbols that perform operations on one or more values. One such operator is the || (logical OR) operator, which can be 6 min read What is the Infinity Global Property in JavaScript ? In JavaScript, the Infinity global property represents positive infinity, which is a numeric value that is greater than any other numeric value. It can be used to represent values that are too large to be represented with the Number data type or as a result of operations like division by zero. Also, 2 min read Right Shift (>>) Bitwise Operator in JavaScript JavaScript bitwise right shift operator is used to operate on two operands where the left operand is the number and the right operand specifies the number of bits to shift towards the right. A copy of old leftmost bits is maintained and they have added again the shifting is performed. The sign bit i 2 min read What is JavaScript >>> Operator and how to use it ? The JavaScript >>> represents the zero-fill right shift operator. It is also called the unsigned right-bit shift operator. It comes under the category of Bitwise operators. Bitwise operators treat operands as 32-bit integer numbers and operate on their binary representation. Zero-fill right 3 min read What does '...' mean in JavaScript? The '...' (or 3 dot symbol) in JavaScript is known as the Spread operator or Rest operator based on the usage. This syntax is used to expand the iterable into individual elements such as arrays, objects, etc.Syntax for Spread Operatorcosnt a1 = [ 10, 20, 30, 40 ];const a2 = [ ...a1, 50]; // Extracti 4 min read Explain the 'double negative' Trick in JavaScript In programming, a "double negative" is a technique used to convert a value to its corresponding boolean value. This technique involves using two negation operators (!) to negate a value twice, resulting in a boolean representation of the original value. The double negative technique is often used to 2 min read What does !== undefined mean in JavaScript ? In JavaScript, !== is a strict inequality operator, and undefined is a special value representing the absence of a value or the lack of an assigned value to a variable. The !== operator checks for both value and type equality, and !== undefined is used to verify if a variable is not equal to the und 1 min read AND(&) Bitwise Operator in JavaScript JavaScript Bitwise AND(&) operator is used to compare two operands by performing an AND operation on the individual bits and returning 1 only if both the bits are one. The AND(&) Operator has a lot of real-world applications and the most famous one is to check whether a number is even or odd 2 min read Like