Skip to content

Instantly share code, notes, and snippets.

@hafeez-syed
Last active April 26, 2016 03:50
Show Gist options
  • Save hafeez-syed/6830f5069a50e398c0deb11482e68e79 to your computer and use it in GitHub Desktop.
Save hafeez-syed/6830f5069a50e398c0deb11482e68e79 to your computer and use it in GitHub Desktop.
var x = 100,
y = 012, // Octal number which is equal to 10
z = 002;
console.log(x+y+z); // 112 not 114
___________________________________________________________________________
var x = 100,
y = 0x12, // Hexadecimal number which is equal to 18, 0x10 = 16, 0x20 = 32, 0x30 = 48
z = 002;
console.log(x+y+z); // 120
___________________________________________________________________________
____________________________ERROR__________________________________________
___________________________________________________________________________
Below two will throw an error because of 'use strict';
'use strict';
var x = 100,
y = 012, // Octal number which is equal to 10
z = 002;
console.log(x+y+z); // 112 not 114
*******************************************************************************************
var x = 100,
y = 0x12, // Hexadecimal number which is equal to 18, 0x10 = 16, 0x20 = 32, 0x30 = 48
z = 002;
console.log(x+y+z); // 120
___________________________________________________________________________
____________________________ERROR FIX______________________________________
___________________________________________________________________________
To fix the above two scenarios we will use parseInt
'use strict';
var x = 100,
y = parseInt(12, 8), // Octal number which is equal to 10
z = 002;
console.log(x+y+z); // 112 not 114
*******************************************************************************************
var x = 100,
y = parseInt(12, 16), // Hexadecimal number which is equal to 18, 0x10 = 16, 0x20 = 32, 0x30 = 48
z = 002;
console.log(x+y+z); // 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment