0

I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for reference.

In C++, I have

#define FIRST_THING 0x0001
#define SECOND_THING 0x0002
...

What do these values mean? And how would I define this in Javascript? Things break if I try to just use 0x0001 and such, so could I just do

var FIRST_THING = 1
var SECOND_THING = 2

or is that completely different?

Kalina
  • 5,504
  • 16
  • 64
  • 101

3 Answers3

5

0x0001 is an integral constant in base 16, i.e., hexadecimal. It is still 1 in base 10. So yes, your example is equivalent, but do you know how to mentally parse 0xBC? If not then you need to study up on arbitrary base arithmetic or at least get comfortable with hex as any programmer should know this stuff.

Sometimes it is easier to view numbers in hex form as they represent bit patterns. In hex, two digits correspond to a byte, so you know at a glance that 0xFF is 255 base 10 and 11111111 base 2. Work on some lower level projects for a while and it will become second nature.

In your C++ example the integral constants are textually replaced by the preprocessor (i.e., all occurrences of FIRST_THING are replaced by 0x0001 before the code is compiled), you don't have such a tool in javascript, so just assign the values to variables directly.

You cannot create 'constants' in javascript, so it's up to you to make sure that you don't change them. However, you can simply write

firstThing = 0x0001;

And it will work just as the C++ example does, i.e., firstThing takes on the value of 1.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • They're not "textually replaced" at all. Both hex and decimal are valid formats for an integral literal, in _both_ languages. – Lightness Races in Orbit Feb 01 '12 at 19:37
  • 1
    @LightnessRacesinOrbit: Eh? I was referring to the `#define` and what the preprocessor is doing. Perhaps I wasn't clear enough. – Ed S. Feb 01 '12 at 19:38
  • Oh, sorry, of course :) I think the OP seemed to think that the hex literals _themselves_ were a problem, though, and didn't really care about the preprocessor. – Lightness Races in Orbit Feb 01 '12 at 19:39
  • @LightnessRacesinOrbit: np, if it confused you then it was bound to confuse the OP. I tried to clear that up a bit. – Ed S. Feb 01 '12 at 19:40
  • I think the question is polarizing. The answers so far all focus on "how do I do a macro in Javascript?" whereas I'm still convinced the question is "how can I use these hex literals in Javascript?" (to which the answer is "er just write them". Either way, poor question to be so unclear! – Lightness Races in Orbit Feb 01 '12 at 19:41
  • @LightnessRacesinOrbit: Yeah, I added another line at the end in attempt to (hopefully) clear up any confusion and ensure the OP that you can certainly use a hex constant in javascript. – Ed S. Feb 01 '12 at 19:43
  • +1 This is now a very good answer, covering multiple possible angles of an unclear question. – Lightness Races in Orbit Feb 01 '12 at 19:44
2

In the C++, those are preprocessor constants in hexadecimal.

In javascript, there is no preprocessor.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

have a look at Are there constants in JavaScript?

There aren't any constants, but convention can help.

Community
  • 1
  • 1
Scott M.
  • 7,313
  • 30
  • 39