Data Types And Operators

chloroplastic
Learning to code bit by bit
3 min readOct 4, 2018

In JS data types include: numbers, strings (of one or more characters), booleans (true vs false), the ‘undefined’ value, the ‘null’ value, symbols (unique identifiers), and objects (complex data structures). Apart from objects, which include functions, all other data types are considered “primitive”: this means that their values can only contain a single thing.

You can easily convert data types: it’s possible to convert them to numbers with Number(value), to strings with String(value), and to booleans with Boolean(value). The ‘typeof’ operator allows you to figure out what kind of type is stored in your variable, for example: typeof 3 // "number".

parseInt() and parseFloat()

  • parseInt(value) parses a leading number to an integer, and it cannot return a floating point number. It can take a second argument, allowing you to specify the base you want to use when interpreting the number.
  • parseFloat(value) parses a number to a floating point number, making sure the decimal part gets interpreted as well. It only parses numbers to base 10.

The plus operator

The plus “+” operator is usually used to sum numbers, but in JS it is also used to concatenate (merge) strings and numbers into new strings. That means, for instance, that “string” + number will produce a string, independently of the order of the two operands. This doesn’t happen with other operators, such as “-”, “/”, “*”, and “=”; they all can only work with numbers.

If the plus operator “+” is applied to a single value that is NOT a number, then it will work in the same way as Number(value), converting said value into a number. For example: alert(+false); // 0.

Order matters

The order in which operators are executed, also called precedence, is the following:

  • When unary (applied to single numbers) “+” and “-” are the operators with the highest priority.
  • The next operators to be executed are “*” and “/”.
  • When applied to multiple numbers “+” and “-” have a low priority.
  • Finally, the last operator to be executed is the assignment operator “=”.

Some other operators include:

  • Assignment “=”: it returns a value (assigns the value to a variable and then returns it).
  • Exponentiation “**”: for example (3 ** 2) will return ³².
  • Remainder “%”: it returns the integer of a division.
  • Increment “++” and decrement “--“: they respectively increase and decrease the variable’s value by 1. If placed before the variable they return the increased/decreased value, whereas if placed after the variable they return the initial value.

TIL of the day:

  1. To execute your JS code in strict mode, you have to use the “use strict” directive. You can either insert it on top of your page (making sure that the whole script will be modifiable) or, more rarely, before a specific function. It is good practice to start scripts with “use strict” in order to avoid errors (e.g. undeclared variables).
  2. You should generally write the name of your hard-coded constants in upper case.

--

--