DEV Community

Cover image for Chapter 1 : Values, Types, and Operators in JavaScript - a quick ride
Kushank Sriraj
Kushank Sriraj

Posted on

Chapter 1 : Values, Types, and Operators in JavaScript - a quick ride

This the first blog in the series of upcoming blogs for Team Tanay EJS Challenge.

You've read the title and know what to learn, so what are we waiting for? Dive in.

Values in JS

A value can be better understood as the most atomic type of data that is used in the program. Simply, call it datatype.
Lets look at the values that JS allows us to use.

NUMBERS

This is used to represent numerical value. It can represent integer, decimal and, exponential numbers.

var n = 50;
Enter fullscreen mode Exit fullscreen mode

The number is represented by 64 bits in the memory, i.e., the variable n occupies 64 bits in the memory once defined.

Similarly, we can use decimals like this:

var d = 2.5;
Enter fullscreen mode Exit fullscreen mode

And using the scientific notation, we can write 25000 as:

var n = 2.5e4;
Enter fullscreen mode Exit fullscreen mode

Representing numeric values is very natural in JavaScript. Just like maths. Hang on! I won't talk about maths anymore.

Arithmetics on numbers

Again, it's simple and natural. The four basic operations, +, -, * and, / are the same.

var plus     = 50 + 50;  //results to 100
var minus    = 50 - 50;  //results to 0
var multiply = 50 * 50;  //results to 2500
var divide   = 50 / 50;  //results to 1
Enter fullscreen mode Exit fullscreen mode

When multiple operators are used together, a precedence is followed:

var confusing = 1 - 4 + 8; //resuts to 5
Enter fullscreen mode Exit fullscreen mode

The / and * operators have same precedence. The same is true for + and - operators also.

It's not confusing if you know the rule of thumb: "When multiple operators with the same precedence appear next to each other, they are applied left to right."

There is an operator to get remainder value also. It's called modulo:

var rem = 10 % 4; //results to 2
Enter fullscreen mode Exit fullscreen mode

In this example, 10 is divided by 4 and the remainder value, i.e., 2 is stored in rem.

Special Numbers

Infinity is a mysterious value. We have positive infinity as Infinity and negative as -Infinity. This value is not mathematically exact and hence it can lead to calculation errors.

Other than this, we have NaN as a value that stands for Not a Number. It is literally used to represent any unexpected calculation results like 0 / 0 or, Infinity - Infinity that is not meaningful.

STRINGS

Strings are used to represent series of characters or text.

Different ways to represent string are:

Using backtick (template literals)

var str = `javascript is fun
           I can code for hours in JS`;
Enter fullscreen mode Exit fullscreen mode

This can go to multiple line and it's printed just like that:

console.log(str);
// Output 
/*
javascript is fun
           I can code for hours in JS
*/
Enter fullscreen mode Exit fullscreen mode

Also, we can enclose strings with ' or " both. But the starting and ending quote must be the same.

var singleQuote = "Some randome text";
var doubleQuote = 'Some more random text';

// If there is an apostrophe then
var names = "Kushank's";
Enter fullscreen mode Exit fullscreen mode

We can join two strings or concatenate with + :

var strJoin = "stringOne " + "stringTwo";

// this results to "stringOne stringTwo"
Enter fullscreen mode Exit fullscreen mode

Moreover, we have a concept of escape characters. It is to print and store values like newline, backslash, etc. We can do that by writing a backslash \ before the character we want to escape.

For escaping newline:

var multiLine = "Line 1\nLine 2;
Enter fullscreen mode Exit fullscreen mode

On printing, Line 1 is printed on first line and Line 2 on the second.

Apart from these, we can also perform small computation inside a template literal:

var stringHack = `two + two = ${2 + 2}`;

// results to : two + two = 4
Enter fullscreen mode Exit fullscreen mode

UNARY OPERATOR

The operators that are used with only one value are called unary operator.

var n = -8;
Enter fullscreen mode Exit fullscreen mode

Here - is a unary operator.

Also, we have a typeof operator that is used to print the datatype of the value.

console.log(typeof "a string"); // > string
console.log(typeof 1.2e4);      // > number
Enter fullscreen mode Exit fullscreen mode

BOOLEAN VALUES

Think of it like a switch with on or true and off or false states. It is very useful in programming.

var truth = true;
console.log(truth);   // > true
Enter fullscreen mode Exit fullscreen mode

COMPARISION

The greater than and less than symbols are used for comparision between two values. The comparision returns a boolean value.

console.log(5 > 3);   // > true
console.log(6 < 20);  // > false
Enter fullscreen mode Exit fullscreen mode

We also have less/greater than or equal to operators:

console.log(7 >= 7);   // > true
console.log(6 <= 1);   // > false
Enter fullscreen mode Exit fullscreen mode

And the equal to == and not equal to != operators:

console.log(NaN == NaN);  // > false
console.log(1 != 2);      // > true
Enter fullscreen mode Exit fullscreen mode

Interestingly, NaN is not equal to NaN as it's a result of some unexpected computaion.

LOGICAL OPERATORS

This would be much easier to understand if you can relate this to logic gates.

AND operator &&: returns true when both values are true else false.

console.log(7 == 7 && 4 < 5);  // > true
Enter fullscreen mode Exit fullscreen mode

OR operator ||: returns true if any one value is true else false.

console.log(7 == 3 || 8 < 5);  // > false
Enter fullscreen mode Exit fullscreen mode

NOT operator !: inverts the boolean value true to false or vice versa.

console.log(!true);  // > false
Enter fullscreen mode Exit fullscreen mode

And one more special operator : the ternary operator

var ternary = (true? "pick true" : "pick false");
console.log(ternary);    // > pick true
Enter fullscreen mode Exit fullscreen mode

EMPTY VALUES

undefined and null are used to represent an empty value. Think of them like a void that has no meaning of it's own. Both of them can be used interchangeably.

AUTOMATIC TYPE CONVERSION

This is a very interesting part and if you've read it until here, please don't miss this. Not having sound understanding of this can lead to unexplained errors or miscalculations.

In order for JavaScript to perform calculations, it does this type conversion to ensure that both of the two values are of the same type.

Some examples :

console.log("4" - 1);  // > 3
Enter fullscreen mode Exit fullscreen mode

Here, the string "4" is converted to the number 4 and then subtraction is performed.
But,

console.log("4" + 1);  // > 41
Enter fullscreen mode Exit fullscreen mode

Here, since + is valid concatenation operator for strings, the number 1 is converted to string "1" and then concantenated with "4".

Sometimes, we don't want any type conversion to happen, and for that, we have === and !== operators.

console.log("4" === 4);  // > false
Enter fullscreen mode Exit fullscreen mode

MORE ON LOGICAL OPERATORS

The logical operators tend to skip the second operation if it's final result is determined by the first operation.

var x = (4 == 3 && console.log('true'));
console.log(x);      // > false
Enter fullscreen mode Exit fullscreen mode

Here, the second operation is not performed at all as the result of AND operation is already determined from the first operation, i.e., false.

Similarly, with || operator, if the first part is true, the second is skipped.
This is called short-circuit evaluation.

One more thing to keep in mind is : 0, NaN, and the empty string ("") are counted as false, while all the other values are counted as true.


Phew! It was a long one. Thanks for reading.
Now, that we have our basics cleared, we can move on to the interesting part : Program Structure which is comming up soon.

If you liked this, hit the heart icon and the unicorn 🦄.

Top comments (0)