Home » JavaScript

JavaScript | How to assign decimal, octal and hexadecimal values to the variables?

Assigning decimal, octal and hexadecimal values to the variables: Here, we are going to learn how can we assign integer values in different format like decimal, octal and hexadecimal to the variables in JavaScript?
Submitted by IncludeHelp, on February 01, 2019

Just like C programming language, we can assign integer value in the different format to the variable.

  • Assigning decimal value: It can be assigned simply without using any prefix.
  • Assigning octal value: It can be assigned by using 0 (zero) prefix with the octal value.
  • Assigning hexadecimal value: It can be assigned using 0x or 0X prefix with the hexadecimal value.

Example:

In the given example, we assign assigning 10 which is in decimal format to the variable a, octal format value 012 which is octal of 10 to the variable b, and hexadecimal format value 0x0A which is a hexadecimal value of 10 to the variable c, and printing all values those will be printed in decimal formats.

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>		
		var a = 10; //decimal value
		var b = 012; //octal value
		var c = 0x0A; //hexadecimal value
		
		//printing values
		document.write("a = " + a + "<br>");
		document.write("b = " + b + "<br>");
		document.write("c = " + c + "<br>");
	</script>
</body>
</html>

Output

a = 10
b = 10
c = 10





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.