0% found this document useful (0 votes)
4 views

Javascript Note2

Calicut University
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Javascript Note2

Calicut University
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JavaScript Data Types

JavaScript supports multiple data types


JavaScript data types are broadly categorized into primitive and non-primitive types.
The primitive data types include Number, String, Boolean, Null, Undefined, and Symbol.
Non-primitive types include Object, Array, and Function.
The latest ECMAScript standard defines eight data types Out of which seven data types are
Primitive(predefined) and one complex or Non-Primitive.
1)Primitive Data Types
The predefined data types provided by JavaScript language are known as primitive data
types. Primitive data types are also known as in-built data types.
Type Description
Number JavaScript numbers are always stored in double-precision 64-bit binary
format IEEE 754.
String JavaScript Strings are made up of a list of characters, essentially an
array of characters.
Boolean Represents a logical entity and can have two values: true or false.
Null This type has only one value: null.
Undefined A variable that has not been assigned a value is undefined.
Symbol Symbols return unique identifiers that can be used as property keys in
objects without colliding with other keys.
BigInt BigInt is a built-in object providing a way to represent whole numbers
larger than 253-1.

2)Non-Primitive Data Types:


 The data types that are derived from primitive data types of the JavaScript language
are known as non-primitive data types.
 It is also known as derived data types or reference data types.
Object: It is the most important data type and forms the building blocks for modern
JavaScript. We will learn about these data types in detail in further articles.
JavaScript Primitive Data Types Examples:
Number:
The number type in JavaScript contains both integer and floating-point numbers. Besides
these numbers, we also have some ‘special-numbers’ in javascript that are:
‘Infinity’,

1|Page
‘-Infinity’, and
‘NaN’.
Infinity basically represents the mathematical ‘?’.
The ‘NaN’ denotes a computational error.

let num = 2; // Integer


let num2 = 1.3; // Floating point number
let num3 = Infinity; // Infinity
let num4 = 'something here too'/2; // NaN
String:
A String in javascript is basically a series of characters that are surrounded by quotes.
There are three types of quotes in Javascript, which are:
let str = "Hello There";
let str2 = 'Single quotes works fine';
let phrase = `can embed ${str}`;
There’s no difference between ‘single’ and “double” quotes in javascript.
Backticks provide extra functionality as with their help of them we can embed variables
inside them.
let name = "Mukul";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, Mukul!
Boolean:
The boolean type has only two values: true and false. This data type is used to store yes/no
values: true means “yes, correct”, and false means “no, incorrect”.

let isCoding = true; // yes


let isOld = false; // no
NULL:
The special null value does not belong to any of the default data types. It forms a separate
type of its own which contains only the null value:

2|Page
let age = null;
The ‘null’ data type basically defines a special value that represents ‘nothing’, ’empty’, or
‘value unknown’.
Undefined Just like null, Undefined makes its own type.
The meaning of undefined is ‘value is not assigned’.
let x;
console.log(x); // undefined
Symbol:
Symbols are new primitive built-in object types introduced as part of ES6.
Symbols return unique identifiers that can be used to add unique property keys to an object
that won’t collide with keys of any other code that might add to the object.
They are used as object properties that cannot be recreated. It basically helps us to enable
encapsulation or information hiding.
let symbol1 = Symbol("Geeks")
let symbol2 = Symbol("Geeks")
// Each time Symbol() method
// is used to create new global Symbol
console.log(symbol1 == symbol2); // False
BigInt:
BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger
than 253-1.
The largest number that JavaScript can reliably represent with the Number primitive is 253-1,
which is represented by the MAX_SAFE_INTEGER constant.
let bigBin = BigInt("0b1010101001010101001111111111111111");
// 11430854655n
console.log(bigBin);
JavaScript Non-Primitive Data Types Examples:
Object:
JavaScript objects are fundamental data structures used to store collections of data.
They consist of key-value pairs and can be created using curly braces {} or the new
keyword.
Understanding objects is crucial, as everything in JavaScript is essentially an object.

3|Page
Object creation:
Using the “object constructor” syntax:
let person = new Object();
Using the “object literal” syntax:
let person = {}; //
JAVASCRIPT OPERATORS:
JavaScript Operators are symbols used to perform specific mathematical, comparison,
assignment, and logical computations on operands.
They are fundamental elements in JavaScript programming, allowing developers to
manipulate data and control program flow efficiently.
Understanding the different types of operators and how they work is important for writing
effective and optimized JavaScript code.

JavaScript Operators
There are various operators supported by JavaScript.
1. JavaScript Arithmetic Operators
2. JavaScript Assignment Operators
3. JavaScript Comparison Operators
4. JavaScript Logical Operators
5. JavaScript Bitwise Operators
6. JavaScript Ternary Operators
7. JavaScript Comma Operators
8. JavaScript Unary Operators
9. JavaScript Relational Operators
10. JavaScript BigInt Operators
11. JavaScript String Operators

4|Page
JavaScript Arithmetic Operators
perform arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/),
modulus (%), and exponentiation (**).
Name
Addition (+)
Addition ‘+’ operator performs addition on two operands. This ‘+’ operator can also be used
to concatenate (add) strings.
Y = “Geeks” + “for” + “Geeks” gives Y = “GeeksforGeeks” Y
Subtraction (-)
Subtraction ‘-‘ operator performs subtraction on two operands.
Y = 5 – 3 gives Y = 2
Multiplication (*)
Multiplication ‘*’ operator performs multiplication on two operands.
Y = 5 * 5 gives Y = 25
Division (/)
Division ‘/’ operator performs division on two operands (divide the numerator by the
denominator).
Y = 5 / 5 gives Y = 1
Modulus (%)
Modulus ‘%’ operator gives a remainder of an integer division.
A % B means remainder (A/B) Y = 5 % 4 gives Y = 1
Exponentiation (**)
Exponentiation ‘**’ operator give the power of the first operator raised to the second
operator.
Y = 5 ** 3 gives Y = 125
Increment(++)
Increment ‘+ +’ operator increases an integer value by one.
let A = 10 and Y = A + + then A = 11, Y=10
Decrement (–)
Decrement ‘- -‘ operator decreases an integer value by one.

let A = 10 and Y = A – – then A = 9, Y=10

5|Page
Unary (+)
Unary ‘+’ is the fastest and preferred way of converting something into a number
+a means a is a positive number
Negation (-)
Negation ‘-‘ operator gives the negation of an operand.
-a means a is a negative number
JavaScript Assignment Operators
The assignment operation evaluates the assigned value. Chaining the assignment operator is
possible in order to assign a single value to multiple variables
Assignment (=)
This operator assigns the right operand value to the left operand.
If A = 10 and Y = A then Y = 10
Addition Assignment (+=)
Sums up left and right operand values and then assigns the result to the left operand.
Y += 1 gives Y = Y + 1
Subtraction Assignment (-=)
It subtracts the right side value from the left side value and then assigns the result to the left
operand.
Y -= 1 gives Y = Y – 1
Multiplication Assignment (*=)
It multiplies a variable by the value of the right operand and assigns the result to the variable.
Y *= A is equivalent to Y = Y * A
Division Assignment (/=)
It divides a variable by the value of the right operand and assigns the result to the variable.
Y /= A is equivalent to Y = Y / A
Modules/Remainder Assignment (%=)
It divides a variable by the value of the right operand and assigns the remainder to the
variable.
Y %= A is equivalent to Y = Y % A

Exponentiation Assignment (**=)

6|Page
This raises the value of a variable to the power of the right operand.
Y **= A is equivalent to Y=Y ** A
Left Shift Assignment (<<=)
It moves the specified amount of bits to the left and assigns the result to the variable.
Y <<= A is equivalent to Y=Y << A
Right Shift Assignment (>>=)
It moves the specified amount of bits to the right and assigns the result to the variable.
Y >>= A is equivalent to Y = Y >> A
Bitwise AND Assignment (&=)
: It does a bitwise AND operation on the operand, and assigns the result to the variable.
Y &= b is equivalent to Y = Y & A
Bitwise OR Assignment (|=)
It does a bitwise OR operation on the operand, and assigns the result to the variable.
Y |= A is equivalent to Y= Y | b
Bitwise XOR Assignment (^=)
It does a bitwise XOR operation on the operand, and assigns the result to the variable.
Y ^= A is equivalent to Y= Y ^ A
JavaScript Comparison Operators
Comparison operators are mainly used to perform the logical operations that determine the
equality or difference between the values.
Equality (==)
Compares the equality of two operands. If equal then the condition is true otherwise false.
Y = 5 and X = 6 Y = = X is false.
Strict equality (===)
Compares the equality of two operands with type. If both value and type are equal then the
condition is true otherwise false.
Y = 5 and X = ‘5’ Y = = = X is false.
Inequality (!=)
Compares inequality of two operands. True if operands are not equal.
let X = 10 then X ! = 11 is true.
Strict inequality (!==)

7|Page
Compares the inequality of two operands with type. If both value and type are equal then the
condition is true otherwise false.
let X = 10 then X ! == ’10’ is true.
Greater than (>)
This operator checks whether the left side value is greater than the right side value. If yes
then it returns true otherwise it returns false.
let X = 10 then X > 11 is false.
Less than (<)
This operator checks whether the left side value is less than the right side value. If yes then it
returns true otherwise it returns false.
let X = 10 then X < 11 is true.
Greater than or Equal to (>=)
This operator checks whether the left side operand is greater than or equal to the right side
operand. If yes then it returns true otherwise it returns false.
let X = 10 then X > = 11 is false.
Less than or Equal to (<= )
This operator checks whether the left side operand value is less than or equal to the right side
operand value. If yes then it returns true otherwise it returns false.
let X = 10 then X < = 10 is true.
JavaScript Logical Operators
JavaScript Logical Operators perform logical operations: AND (&&), OR (||), and NOT (!),
evaluating expressions and returning boolean values.
Logical AND (&&)
It checks whether two operands are non-zero (0, false, undefined, null, or “” are considered as
zero), if yes then return the last operand when evaluating from left to right
Y = 5 and X = 6 Y && X is 6.
Logical OR (||)
It checks whether two operands are non-zero (0, false, undefined, null, or “” is considered as
zero), if yes then return the first operand when evaluating from left to right.
Y = 5 and X = 0 Y || X is 5.
Logical NOT (!)
It reverses the boolean result of the operand (or condition).
Y = 5 and X = 0 !(Y || X) is false.

8|Page
JavaScript Bitwise Operators
The bitwise operator in JavaScript is used to convert the number to a 32-bit binary number
and perform the bitwise operation. The number is converted back to the 64-bit number after
the result.
Bitwise AND (&)
The operator returns true only if both the operands are true
A = 6, B=1 A&B = 0
Bitwise OR (|)
The operator returns true even if one of the operands is true
A = 6, B=1 A|B = 7
Bitwise XOR (^)
The operator returns true if both operators are distinct
A = 6, B=1 A^B = 7
Bitwise NOT (~)
This operator is used to invert the boolean value of the operand
A = 6 ~A = -7
Bitwise Left Shift (<<)
In this two operators are used where the first operand is the number and the second operand is
the number of bits to shift to the left.
A = 6, B=1 A<<B = 12
Bitwise Right Shift (>>)
In this two operators are used where the first operand is the number and the second operand is
the number of bits to shift to the right.
A = 6, B=1 A>>B = 3
Zero Fill Right Shift (>>>)
It is same as a bitwise right shift the only difference is that overflowing bits are discarded.
A = 6, B=1 A>>>B = 3
JavaScript Ternary Operators
The ternary operator has three operands. It is the simplified operator of if/else.
Ternary Operator (?:)
It is like the short form of the if-else condition.
Y = ? A : B If the condition is true then Y = A otherwise Y = B

9|Page
JavaScript Comma Operators
Comma Operator (,) mainly evaluates its operands from left to right sequentially and returns
the value of the rightmost operand.
comma operator (,)
When a comma operator is placed in an expression, it executes each expression and returns
the rightmost expression.
Expression1, Expression2, Expression3, …so on
JavaScript Unary Operators
A unary operation is an operation with only one operand.
JavaScript typeof
It returns the operand type, The possible types that exist in javascript are undefined, Object,
boolean, number, string, symbol, and function.
typeof variable
Delete
This operator is more specifically used to delete JavaScript object properties.
delete object
// or
delete object.property
// or
delete object[‘property’]
JavaScript Relational Operators
JavaScript Relational operators are used to compare its operands and determine the
relationship between them. They return a Boolean value (true or false) based on the
comparison result.
in
The in operator returns true if the specified property is in the specified object.
propNameOrNumber in objectName
instanceof
The instanceof operator returns true if the specified object is of the specified object type.
objectName instanceof objectType
JavaScript BigInt Operators

10 | P a g e
JavaScript BigInt operators support arithmetic operations on BigInt data type, including
addition, subtraction, multiplication, division, and exponentiation. Most operators that can be
used between numbers can be used between BigInt values as well.
BigInt
It returns a new BigInt object that represents the original value.
BigInt(value);
JavaScript String Operators
JavaScript String Operators include concatenation (+) and concatenation assignment (+=),
used to join strings or combine strings with other data types.
concatenation operator (+)
It concatenates two string values together, returning another string that is the union of the two
operand strings.
str1 + str2

11 | P a g e

You might also like