Saturday 22 December 2018

How to represent octal numbers in JavaScript?

Octal numbers are represented by a leading 0 followed by digits between (0 – 7).

HelloWorld.js
var x = 0123; //83 in decimal
var y = 011; //9 in decimal

console.log("x : " + x);
console.log("y : " + y);

Output
x : 83
y : 9

Usage of "0"-prefixed octal literals and octal escape sequences are deprecated in ECMAScript 5. You should use "0o" prefix instead.

Try to run below program, you can see the error.

HelloWorld.js
"use strict"
var x = 0123; //83 in decimal
var y = 011; //9 in decimal

console.log("x : " + x);
console.log("y : " + y);



To get rid off the error use the prefix 0o.


HelloWorld.js

"use strict"
var x = 0o123; //83 in decimal
var y = 0o11; //9 in decimal

console.log("x : " + x);
console.log("y : " + y);


Previous                                                 Next                                                 Home

No comments:

Post a Comment