[Go to site: main page, start]

0% found this document useful (0 votes)
5 views4 pages

Day2 JavaScript

The document explains various operators in JavaScript, including increment/decrement, assignment, logical, string concatenation, and type conversion. It provides examples for each operator type, illustrating their usage and behavior. Additionally, it highlights automatic type coercion in JavaScript when performing operations between different data types.

Uploaded by

jeevavaishnavi7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Day2 JavaScript

The document explains various operators in JavaScript, including increment/decrement, assignment, logical, string concatenation, and type conversion. It provides examples for each operator type, illustrating their usage and behavior. Additionally, it highlights automatic type coercion in JavaScript when performing operations between different data types.

Uploaded by

jeevavaishnavi7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Post & Pre Increment/Decrement Operators


These operators are used to increase (++) or decrease (--) the value of a variable by 1. The
difference between pre and post is when the increment/decrement happens.
Pre-increment / Pre-decrement
 ++x → increments x before using it.
 --x → decrements x before using it.
Example:
let x = 5;
let y = ++x; // pre-increment: x becomes 6, then assigned to y
[Link](x); // 6
[Link](y); // 6
Post-increment / post-decrement
 x++ → increments x after using it.
 x-- → decrements x after using it.
Example:
let x = 5;
let y = x++; // post-increment: y gets 5, then x becomes 6
[Link](x); // 6
[Link](y); // 5

2. Assignment Operators
Assignment operators are used to assign values to variables. The most basic is =, but there are
shorthand operators combining arithmetic with assignment:

Operator Meaning

= Assign

+= Add and assign

-= Subtract and assign

*= Multiply and assign

/= Divide and assign

%= Modulus and assign

Example:
let x = 10;
x += 5; // equivalent to x = x + 5
[Link](x); // 15

x *= 2; // equivalent to x = x * 2
[Link](x); // 30

3. Logical Operators
Logical operators are used to combine or invert boolean values.

Operator Symbol Description

AND && True if both operands are true

OR `

NOT ! Inverts the boolean value

Example:
let a = true;
let b = false;

[Link](a && b); // false


[Link](a || b); // true
[Link](!a); // false

4. Concatenate & Template String


String Concatenation
Combining strings using + operator.
let firstName = "Jeeva";
let lastName = "Vaishnavi";
let fullName = firstName + " " + lastName;
[Link](fullName); // Jeeva Vaishnavi

Template String (Template Literal)


 Uses backticks `.
 Supports variable interpolation with ${variable}.
 Can span multiple lines easily.
let age = 20;
let message = `Hello ${firstName}, you are ${age} years old.`;
[Link](message);
// Hello Jeeva, you are 20 years old.

let multiLine = `This is line 1


This is line 2`;
[Link](multiLine);

5. Type Conversion
Converting one data type to another.
String to Number
let str = "123";
let num = Number(str);
[Link](num); // 123
[Link](typeof num); // number
Number to String
let x = 456;
let strX = String(x);
[Link](strX); // "456"
[Link](typeof strX); // string
Boolean Conversion
let val = 0;
[Link](Boolean(val)); // false

let val2 = "hello";


[Link](Boolean(val2)); // true

Automatic (Type Coercion)


JavaScript sometimes converts types automatically:
[Link]("5" - 2); // 3 (string converted to number)
[Link]("5" + 2); // "52" (number converted to string)

You might also like