DEV Community

mimansha-swarup
mimansha-swarup

Posted on

Learn Value, Types and Operators in JavaScript

So in this series of blog I will be blogging chapters of Eloquent JavaScript

Let's see what is data and how machine interpret data.We store our data on Electronic devices for example we stored a image on a computer now this image is been stored in a long sequence of bits
and bits have two value 0 and 1, true and false,high volt and low volt etc

Table of Content

  1. Numbers
  2. Special Numbers
  3. String
  4. Boolean Value
  5. Logical Operator
  6. Ternary Operator
  7. Empty Value

Numbers

name itself is self explanatory that this means numerical values for example 13 is a number.
JavaScript use 64 bit to store number value, the largest number that can be stored is 2 power 64 i.e 2^64 which is around 18000000000000000000 in short 18 followed by 18 zeros but there is a catch this is theoretical but the problem is js also need to store -ve values also so for that JS use one bit to store - or +if number is -ve it will have 1 and even if value is +ve 0 will be used

FunFact : In real JS can store upto 9000000000000000 which is 9 followed by 15 zeros

Special Number

There are 3 special numbers in JS who are termed as number but are nothing like number. These are

  • Infinity
  • - Infinity
  • NaN Infinity Name itself says that this value refers to infinite value and - Infinity means negative of infinite

FunFact: If we subtract Infinity from Infinity we will get NaN Infinity-Infinity

NaN means not a number this you will get NaN as result when a calculation has done but result is not meaningful for eg: 0/0
0/0

Strings

so string are basically any text whether it's a letter ,word or sentence wrapped with single quotes or double quotes back tick any value warped with above characters are considered as String eg:

" This is a String !"
'This is a String too.'
`This is String with Back Tick`
Enter fullscreen mode Exit fullscreen mode

There are some special charterers for string one is newline character( \n )

console.log("example of\nnew line character")
/*example of
new line character*/
Enter fullscreen mode Exit fullscreen mode

on string we can't perform any arithmetic operation but anyway we van add strings this is called as string concatenation
"app" + "le" this will result "apple"

Back tick string are called template literal
using Back tick we can print the value of variable inside string this can be done using ${} in between curly braces you need to write variable name eg:

`The area of square is ${variableName}`
Enter fullscreen mode Exit fullscreen mode

Boolean

boolean are the true false value. If the expression is correct then it will give True and if wrong False value is given by the expression on evaluation

console.log(3>1) True
console.log(3<1) False
Enter fullscreen mode Exit fullscreen mode

FunFact: Js try to accept every calculation/command you give even if its odd and try to return a value for eg: if you add string with a number '3'+1 o/p will be 31

Logical Operators (and,or,not)

  • and(&&) : if both the value are true it will give true if one or more value is false it will return false
  • or(||) : if one or more value is true it will return true if both the value are false it will return false
  • not(!) : if value is true it will return false if value is false it will return true > Note: || has lowest Priority after that && and after that comparison operator(==,<,>,<=,>=,!=) have third lowest priority and then the rest example:

1 + 1 == 2 && 10 * 10 > 50 first arithmetic operators will be evaluated

Note: in arithmetic *,/,% operators have highest priority then +,-

10*10 will be evaluated

1 + 1 == 2 && 100 > 50 now 1+1 will be evaluated

2== 2 && 100> 50 next comparison operators will be evaluated 2==2 and 100>50

True && True

True

Ternary Operator

ternary operators are kind of shortcut of Conditional statement or one liner of if else Syntax:

Condn ? st1 : st2
Enter fullscreen mode Exit fullscreen mode

if the condition is evaluated as true statement 1 will be executed and if it is evaluated as false statement 2 will be executed

Empty Values

these are another two special values null and undefined which indicate absence of meaningful value

Automatic Type Conversion

js automatically tries to convert type of variable on operation and it tries to accept all kind of operations example:

js operations

and in this process JS tries to do operation on odd values like it's adding sting to a number and giving output as string so we will not be getting expected type of value this is called as type coercion

console.log(null || "name")
// name
console.log("hello" || "name")
// hello
Enter fullscreen mode Exit fullscreen mode

Here the || operator will return the left value if it can be converted to true and will return the right value otherwise.
That's why at first we are getting name and in second line output is hello

Top comments (0)