Tuesday, September 29, 2020

JS Datatypes | Operators

JavaScript Datatypes


Datatypes in javascript mean the type of data stored in a variable.

As JavaScript is loosely typed scripting language, there is no typecast in javascript. JS supports dynamic typing. We can create any type of data using a single variable i.e. var.

var means a variable that can store any type of data. Datatype of variable is not declared.

Declaring var means creating a new variable in memory with the variable name after white-space. Assignment Operator (=) means assigning value to variable declared.

We can also use const and let to declared variables.

Datatypes in JavaScript

  1. Primitive Data Types
  2. Reference Data Types

Primitive Datatypes in JavaScript

Primitive datatypes are the basic or common data types in javascript. Like stringnumbersbooleanundefined, and null. They are very commonly used data types.

var is used to declare primitive datatypes in javascript.

Primitive Type

Data Meaning

var x;

undefined

var x=undefined;

undefined

var x=null;

null type data

var x=3;

Data Type is number.

var x=3.5

Data Type is number with decimal

var x="3"

Data Type is string

var x='3'

Data Type is string

var x="HELLO"

Data Type is string

var x=true

Boolean data type

var x=false;

Boolean data type

Strings

Anything written in single and double quotes is strung in javascriptStrings are used to store name, email, city name, password, etc in javascript. JavaScript String

     var name="js string";      

Numbers

JavaScript Numbers are used to perform Arithmetic Operations (+,-,*,/,%). Numbers are written without quotes. JavaScript Numbers.

        var num=20;

Boolean

JavaScript Boolean are true and false. Booleans are used in conditions, comparison etc.

        var t=true;


    var f=false;    

Undefined

JavaScript Undefined means any variable whose value is not assigned yet. Anything variable whose value is not assigned is undefined.


     var u;

     var t=undefined;  

Null

JavaScript null is a special object with empty value. null is used where value is defined, but still it is not there. It is also used in exception handling.

        var u=null;


Reference Data Type in JAVASCRIPT

Reference are datatypes based on primitive. Like ArrayObject and Functions. Everything is JavaScript is either a primitive datatype or Object. Even Arrays and Functions are objects, but they are build-in objects.

var is also used to declare reference datatypes.


Reference Data Type

Meaning

var x=[ "Jan", "Feb", "Mar" ]

Array

var x={ "name" : "ABC", "age" : "22", "gender" : "male" }

Object

var x=function(x,y){ return x+y;}

Function Expression

function sum(x,y){ return x+y;}

Function Declaration

var x=new Date();

Date

var x=/^[0-9]{6}$/

Regex


typeof Operator

typeof operator in javascript is used to check datatype of a variable. It can return string, number, boolean and undefined. For reference type and null, typeof operator will return object.

    var x;                 // undefined
    var y=9;               // number
    var z="Tech Altum";    // string
                                                                           
    typeof(x) and typeof x will return undefined,
    typeof(y) and typeof y will return number,
    typeof(z) and typeof z will return string;


JavaScript Operators


Javascript Operators are used to assign, add, subtract, compare variables value. JavaScript is having Arithmeticlogicalassignment and comparison operators.

JavaScript has binaryunary, and ternary operators.

Binary operators required two operands, one before and one after the operator.  x+y=z

Unary operators required only one operand, either before or after the operator.  i++

Type of Operators in Javascript

  1. Arithmetic Operators
  2. Logical Operators
  3. Assignment Operators
  4. Comparison Operators
  5. Conditional Operator
  6. Bitwise Operators

Arithmetic Operators

An Arithmetic Operator is used to perform Arithmetic operations between values. Like Addition, Subtraction, Multiplication, Division etc

Arithmetic operators in JavaScript

Operator

Description

Example

+

Addition

2+3=5

-

Subtraction

5-3=2

*

Multiply

2*3=6

/

Divide

6/3=2

%

Modulus, check reminder

6%3=0

++

Increment, y++ means y = y+1

var y=2; ++y; y=3

--

Decrement, y-- means y = y-1

var y=2; --y; y=1

Logical Operators

Logical Operators are used to check logic between two operators. and (&&)or (||) and not (!) are logical operators.

Logical Operators in JavaScript

Operator

Description

Example

&&

and, when both are correct

2 < 5 && 2> 1 is true

||

or, when any one is correct

var x=2, 2>5 || x==2 is true

!

not

!(2==3) is true

Assignment Operators

Assignment operators are used to assign some value to js variables. =, +=, -=, *=, /= are all assignment operators in javascript.

Assignment Operators in JavaScript

Operator

Description

Example

=

Assignment

x=2; means x is 2

+=

Addition Assignment

var x=2; x+=2 means x=x+2

-=

Subtraction Assignment

var x=2; x-=2 means x=x-2

*=

Multiplication Assignment

var x=2; x*=2 means x=x*2

/=

Division Assignment

var x=2; x/=2 means x=x/2

Comparision Operators

Comparison operators are used in a statement to compare two values.

Comparison Operators in JavaScript

Operator

Description

Example

==

Equal to

2=="2" is true

===

Strict equal to

2==="2" is false

!=

not equal

2!=1 is true

!==

not strict equal

2!=="2" is true

> 

greater than

2> 5 is false,
& 2 <5 is true

>=

greater than or equal to

3>=3 is true

< 

less than

1< 3 is true

<=

less than or equal to

2<=2 is true

Conditional operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is false. This operator is frequently used as a shortcut for the if statement.

 function getFee(isMember) {

    return (isMember ? '$2.00' : '$10.00');
  }
  console.log(getFee(true));
  // expected output: "$2.00"
  
  console.log(getFee(false));
  // expected output: "$10.00"
  
  console.log(getFee(null));
  // expected output: "$10.00"
  

Bitwise operator 

Like C, C++, Java, Python, and various other languages, JavaScript also supports bit-wise operations. In JavaScript, a number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and perform the operation and convert back the result to a 64-bit number.

<script>  
    var a = 4;  
    var b = 1;  
      
    document.write("A & B = " + (a & b) + '<br>');  
    // expected output: 0

    document.write("A | B = " + (a | b) + '<br>');  
    // expected output: 5

    document.write("~A = " + (~a) + '<br>');  
    // expected output: -5
</script>