Values, Types and Operators

Values, Types and Operators

Learning JavaScript is a very important skill that you should know if you want to start your web development journey. And every journey starts with the basics of the language. It's very important to get a grasp on basics before going into deep concepts of any language.

raw.gif

What is Values?

  • As we already know that everything in the computer is stored in the form of bits. So, values is just a chunk of bits that represent some piece of information.

  • They play a different role according to their types.

Now, you guys must be thinking what are the types of values that exist in JS. Are they the same as in other languages?

source.gif

Here's the answer:

Types of Values

  • Numbers: As we can already guess, it is of numeric type. Numbers can be whole numbers ( 3 ), fractional numbers ( 10.11 ) and for large numbers we can use exponent by adding e:

4.56e10 i.e. 4.56 x 10^10

There exist some special numbers also: Infinity, -Infinity, and NaN (Not a Number). Generally, we get this result when trying to do something unusual like 0/0.

const a = 0/0;
a
Result : NaN

Now, you must be thinking about what is this "const" word. I have never heard it. We will understand it later.

  • Strings: It is used to represent text. Anything written in between double quotes, single quotes, or backtick quotes are strings.
"This is my first blog."
'This is my first blog.'
`This is my first blog.`

To put characters which has some special meaning such as newline or tab character we can use "\" to it to make them work.

"This is my \n first blog."
Output: This is my
        first blog.

Strings written in between double quotes and single quotes work very much similar but backtick quotes provide some flexibility. So, let's learn something more about backtick quotes.

Backtick-quoted strings also called template literals. Apart from strings, they can embed values using ${}. If we add something in between it, its value got computed, converted to a string, and inserted there again.

`My age is ${44/2}.`
Output: "My age is 22."
  • Boolean: It gives two results only either "true" or "false".
console.log(3 > 2)     true
  • Empty Values: There are two keywords that denote empty values i.e. null and undefined. They themselves don't carry any information and can be used interchangeably.

Bingo! Now you know almost everything about values.

200.gif

Now let's get started with the operators.

What are Operators?

Operators are symbols that are used to perform operations on operands and in return, it gives back some result. Operators, operands too much to handle no?? Let's understand it with a simple example.

3 + 4 = 7

In the above example, "3" and "4" are operands on which operation is performing. "+" is an operator which is performing the operation and "7" is the result of the operation.

Types of Operators

  • Arithmetic: The basic mathematics operations that we perform in our daily life i.e. +, -, * and / are called arithmetic operators. There is one more operator involved in it called a modulo operator denoted by "%" which performs the remainder operation.
4 % 2 gives result 0

+ operator also uses in string concatenation. Concatenation means interconnecting two or more strings together.

hello + world
this gives the result `helloworld`.
  • Unary: It takes only one operand to perform the operation.

    • typeof: Not all operators are symbols. Some are written as words also and typeof is one of them. It gives the "type" of the value.

      console.log(typeof "hello")
      output: string
      
    • -: It is a logical operator and is used to negate the value i.e. it just converts the positive value into a negative value.

    • !: It is used to negate the value logically or we can say it toggles the value. Example: !true yields false and !false yields true.
  • Logical: JavaScript supports three logical operators: and, or and not. They are binary operators meaning they take two operands and produce a result.

    • Logical and: It is denoted by && and it gives the true result only if both the values are true.
      console.log(true && true ) gives the result true.
      console.log(true && false ) gives the result false.
      
    • Logical or: It is denoted by || and it gives the true result only if either of the value is true.
      console.log(true || true ) gives the result true.
      console.log(true || false ) gives the result true.
      
    • Logical not: It is denoted by ! and we already learned about it in unary operators.
  • Comparison: It consists of greater than(>), lesser than(<), greater than equal to(>=), lesser than equal to(<=), equal(==), not equal to(!=).
console.log(4 >= 6) yields false.
  • Ternary: It's also called conditional operator. It takes up three values and uses ? and : in it. Condition comes before ? and based on that condition it gives the other two of the result.
console.log(3>4 ? "true" : "false")
output: "false"

Bingo! We completed the operators part too.

bdd5e0f08f764602519b58fd74f33156.gif

That's all, we have successfully completed the first chapter of Eloquent JS and learned about Values, operators, and their types.