13

This is what I want:

>> var uint n = 0;
<- undefined (output by firefox dev console, I don't know what this means)
>> n;
<- 0;
>> n - 1;
<- 4294967295 (2^32-1)

So how to declare a variable, in javascript, so that it is unsigned? Thanks in advance.

Lukas Kitsche
  • 147
  • 1
  • 1
  • 5
  • 1
    There is no built-in unsigned integer type in Javascript. I suppose you could use a typed ArrayBuffer (Uint32Array). – Thilo Nov 30 '16 at 09:01
  • Note, Javascript doesn't *have typed variables*. It is a dynamic language... – juanpa.arrivillaga Nov 10 '18 at 00:34
  • FYI, the `undefined` that appears in the console is just the response to the variable assignment, as the result of that assignment has no value – mwieczorek Aug 26 '19 at 11:31

3 Answers3

22

LK"I

Is this what you mean?

var unsigned = someVar >>>0

JavaScript C Style Type Cast From Signed To Unsigned

slfan
  • 8,950
  • 115
  • 65
  • 78
Harel Ashwal
  • 1,520
  • 1
  • 14
  • 11
11

If you really need them unsigned then check Uint32Array on MDN.

However, since you don't really gain anything from your values being unsigned, perhaps a simple modulus operation would be better?

//Create var as array of length 1
var arr = new Uint32Array(1);
//set first value to 1
arr[0] = 1;
//output contents
console.log(arr);
//substract to "negative"
arr[0] -= 2;
//output contents
console.log(arr);

Modulus

//Object with setter, to make it simpler
var my_obj = Object.create({
  //This is our value
  value: 0
}, {
  //This is our setter
  set: {
    value: function(a) {
      //Make incoming value a modulus of some integer
      //I just use 1000 for the example
      var max = 1000;
      a = a % max;
      //Force a positive
      while (a < 0) {
        a += max;
      }
      //Set value
      this.value = a;
      //Return object regerence for chaining
      return this;
    }
  }
});
console.log("0:", my_obj.value);
my_obj.set(500);
console.log("500:", my_obj.value);
my_obj.set(-0);
console.log("-0:", my_obj.value);
my_obj.set(-1);
console.log("-1:", my_obj.value);
my_obj.set(-100);
console.log("-100:", my_obj.value);
my_obj.set(-0);
console.log("-0:", my_obj.value);

Or with a function:

function modulusMax(value, a) {
  //Make incoming value a modulus of some integer
  //I just use 1000 for the example
  var max = 1000;
  a = a % max;
  //Force a positive
  while (a < 0) {
    a += max;
  }
  //Set value
  value = a;
  //Return object regerence for chaining
  return value;
}

var v = 0;

console.log("0:", modulusMax(v, 0));
console.log("500:", modulusMax(v, 500));
console.log("-0:", modulusMax(v, -0));
console.log("-1:", modulusMax(v, -1));
console.log("-100:", modulusMax(v, -100));
console.log("1100:", modulusMax(v, 1100));
console.log("-0:", modulusMax(v, -0));
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
0

Every number elevated by two is positive, so...

let uint = number =>  Math.sqrt(Math.pow(number, 2));
console.log(uint(-4)); // 4
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 6
    Absolutely do not use this method. It's effectively the same as console.log(Math.abs(-4)) only *way* more computationally intense – Tim Nov 07 '22 at 10:51