JavaScript Basics — Simple Value Types and Operators
Photo by Dan Gold on Unsplash

JavaScript is one of the most popular programming languages in the world. To use it effectively, we’ve to know about the basics of it.

In this article, we’ll look at the basic value types and operators.

Values

Values are the most basic types of data in a JavaScript program.

They’re stored as bits on the computer in memory.

We’ve to store values so that we can work with them later in the program.

Numbers

Numbers are the most basic data type. They store simple numeric values.

There’s no difference between floating-point and integer types.

They are all stored in one data type.

The numbers in JavaScript are 64 bit. This means that there are 64 binary digits or 2 raised to the 64th power number of numbers.

Some bits are used to store the decimal point.

This means that it’s actually 9 quadrillion numbers, position or negative.

The main thing we do with numbers are arithmetic.

We can use operators to do operations with numbers.

For instance, we can write:

1 + 3

or

2 * 3

+ and * are operators. + is for addition and * is for multiplication.

We can prioritize operations with parentheses.

For instance, we can write:

(1 + 3) * 2

to prioritize addition over multiplication.

Without parentheses, multiplication would go first.

Subtraction is done with the - operator and division is done with the / operator.

When there’re no parentheses in an arithmetic expression, then the precedence of the operators is applied by the regular rules of math.

* and / have precedence over + an - .

% stands for the remainder operator.

1 % 2 means that we get the remainder of dividing 1 by 2.

Therefore, 1 % 2 returns 1.

Special Numbers

There are some special values in JavaScript rhat are considered numbers but they don’t act like normal numbers.

Infinity and -Infinity are represent positive and negative infinite values.

Infinity — 2 is still Infinity .

We shouldn’t trust these values too much.

NaN stands for ‘not a number’. However, it’s still of type number.

We’ll get this result when we try to calculate 0 / 0 , Infinity — Infinity or any numeric operations that don’t yield a meaningful result.

Strings

Strings are another basic data type. It’s used to represent text.

They’re written by enclosing quotes.

We can use single quotes, double quotes, or backticks to enclose a string.

As long as the starting and ending delimiter match, we can use any of them.

For example, we can write:

'hello world'

or:

"hello world"

or:

`hello world`

If the backslash character is found in the quoted text, then it’s used for escaping the character.

It won’t be displayed and it’s used to write special characters like newline or tabs.

For instance, we can write:

'hello

'

where

is the newline character.

Strings are also stored as a series of bits on a computer.

They’re encoded into the Unicode character set and then stored on the computer.

Unicode has almost every character we would ever need from any language like Greek, Japanese, numbers, in addition to English characters.

Strings can’t be divided, multiplied pr subtracted, but + can be used on strings for concatenation.

For instance, we can write:

'hello' + 'world'

Strings in double-quotes act the same as if they’re wrapped in single quotes.

Template literals can do a few more tricks. They’re the strings enclosed by backticks.

We can embed expressions into them. For example, we can write:

`half of 50 is ${50 / 2}`

Then we get:

`half of 50 is 25`

as the final result.

Unary Operators

Unary operators operate on one operand.

One of them is the typeof operator for checking the type of a value.

For instance, we can write:

typeof 10

to get 'number' and:

typeof 'y'

to get 'string'.

Photo by Merve Aydın on Unsplash

Binary Operators

Operators that take 2 operands are called binary operators. They’re the ones like the arithmetic operators.

Conclusion

Numbers and strings are the basic types of data. Large quantities of numbers can be stored.

We can work with them with various operators. They’re encoded with Unicode so that we can store lots of characters.

Unary operators take one operand.

JavaScript In Plain English

Enjoyed this article? If so, get more similar content by subscribing to our YouTube channel!
['John Au-Yeung']
['Technology', 'Software Development', 'JavaScript', 'Programming', 'Web Development']