Data Type & Operators in Javascript

Codynn
5 Min Read

Data Types

There are two types of types in JavaScript: primitive types and object types.

Primitive types

Primitive types are:

Numbers: represents numeric values

Strings: represents a sequence of characters

Booleans: represents boolean value either false or true

Symbols: It’s a somewhat unusual data type. The value of a symbol is kept secret and for internal use, after it is created.

non-primitive data types

Object:  represents instance through which we can access members

Any value that’s not of a primitive type (a string, a number, a boolean, null or undefined) is an object.

Null: represents null i.e. no value at all

 Undefined: represents an undefined value

let length = 10;                               // Number

let lastName = “Ak”;                      // String

let x = {firstName:”pujan”, lastName:”Bhusal”};    // Object

Array: represents group or collection of similar data type

RegExp: represents a regular expression

Operators

Operators in JavaScript are symbols that perform operations on operands. Consider the following scenario:

var sum=30+20;  

Here, + is the arithmetic operator and = is the assignment operator.

In JavaScript, there are several different types of operators.

  • Arithmetic Operators: Addition, substraction, multiplication, division, Exponentiation, Modulus, Increment, Decrement(+, -, *, /, **, %, ++, –)
// Arithmetic Operators
let a = 90;
let b = 10;
console.log("The value of a + b is ", a+b);
console.log("The value of a - b is ", a-b);
console.log("The value of a * b is ", a*b);
console.log("The value of a / b is ", a/b);

OUTPUT

The value of a + b is  100

The value of a – b is  80   

The value of a * b is  900    

The value of a / b is  9     

  • Comparison (Relational) Operators:To compare the values of two variables or to compare a given value against a variable(  >, >=, <, <=, ==, !=, =, *=, /=, %=, +=, -=)
// Comparison Operators
let x = 30;
let y = 50;
console.log(x == y);
console.log(x >= y);
console.log(x <= y);
console.log(x > y);
console.log(x < y);

Output

False

False

True

False

True

  • Bitwise Operators:  Bitwise operators perform actions on operands(“1 + 2” the “1” and “2” are the operands and the plus symbol is the operator) that are a set of 32-bit binary digits (zeroes and ones). The result, however, is displayed as a decimal value.
let a = 5 & 5; // output 5
let b = 5 | 2; // 101 | 010 = 111 (output: 7)
let c = 5 ^ 3; // 101 ^ 011 = 110 (output: 6)
let d = 5 << 1; // 101 -> 1010 (output: 10)
let e = 5 >> 1; // 101 -> 010 (output: 2)
let f = 8 >>> 1; // 1000 -> 0100 (output: 4)
console.log(a, b, c, d, e, f);

Output: 5, 7, 6, 10, 2, 4

  • Logical Operators:  A logical operation is a symbol or word that joins two or more information elements. It’s most commonly used to see if a relationship between two terms is true or false.
// Logical Operators
 
// Logical and, or, not
console.log(true && true)
console.log(true && false)
console.log(false && true)
console.log(false && false)
 
console.log(true || true)
console.log(true || false)
console.log(false || true)
console.log(false || false)
 
console.log(!false);
console.log(!true);

Output

True

False

False

False

True

True

False

True

False

  • Assignment Operators: JavaScript variables are assigned values using assignment operators. An assignment operator  used to assigns a value to its left operand based on the value of its right operand.
// Assignment Operators
var b = 1;
var c = b;
c += 2; // c = c + 2
c -= 2; // c = c - 2;
c *= 2;
c /= 2;
console.log(c);

Output

1

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *