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?
0x0001is an integral constant in base 16, i.e., hexadecimal. It is still1in base 10. So yes, your example is equivalent, but do you know how to mentally parse0xBC? 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
0xFFis255base 10 and11111111base 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_THINGare replaced by0x0001before 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
And it will work just as the C++ example does, i.e.,
firstThingtakes on the value of1.