Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

lecture3 of the javascript file system

The document provides an overview of JavaScript operators, including arithmetic, assignment, comparison, string, logical, bitwise, ternary, and type operators. It includes examples of how to use these operators in code, demonstrating their functionality in mathematical and logical computations. Additionally, it covers concepts such as string concatenation and variable swapping.

Uploaded by

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

lecture3 of the javascript file system

The document provides an overview of JavaScript operators, including arithmetic, assignment, comparison, string, logical, bitwise, ternary, and type operators. It includes examples of how to use these operators in code, demonstrating their functionality in mathematical and logical computations. Additionally, it covers concepts such as string concatenation and variable swapping.

Uploaded by

Imtiaz Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JavaScript Operators

Javascript operators are used to perform different types of mathematical and


logical computations.

Examples:
The Assignment Operator = assigns values

The Addition Operator + adds values

The Multiplication Operator * multiplies values

The Comparison Operator > compares values

JavaScript Assignment
The Assignment Operator (=) assigns a value to a variable:

Types of JavaScript Operators


There are different types of JavaScript operators:

 Arithmetic Operators
 Assignment Operators
 Comparison Operators
 String Operators
 Logical Operators
 Bitwise Operators
 Ternary Operators
 Type Operators
JavaScript Arithmetic Operators
Arithmetic Operators are used to perform arithmetic on numbers:

Arithmetic Operators Example


<script>

let x=10;

document.write(x);

</script>

<script>

let a = 3;

let x = (100 + 50) * a;

document.write(x);

</script>

Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
JavaScript Assignment Operators
Assignment operators assign values to JavaScript variables.

The Addition Assignment Operator (+=) adds a value to a variable.

<script>

var x = 10;

x += 5;

document.write(x);

</script>

Operator Example Same As


= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y

Input in java Script


<script>

const num1 =Number( prompt("Enter any number:"));

const num2 = Number(prompt("Enter second number:"));

const num3=text1+text2;

document.write("Addition of text and text2 is: "+num3);

Area of Tringle
<script>

const baseValue = prompt('Enter the base of a triangle: ');

const heightValue = prompt('Enter the height of a triangle: ');

// calculate the area

const areaValue = (baseValue * heightValue) / 2;

document.write("the are of triangle is :"+areaValue);

</script>

Swaping
<script>

//JavaScript program to swap two variables

//take input from the users

let a = prompt('Enter the value a: ');

let b = prompt('Enter the value b: ');

//create a temporary variable

let temp;

//swap variables
temp = a;

a = b;

b = temp;

document.write("value of a :"+a);

document.write("<br>");

document.write("value of b :"+b);

</script>

Swap Example 2
<script>

//JavaScript program to swap two variables

//take input from the users

let a = parseInt(prompt('Enter the value a: '));

let b = parseInt(prompt('Enter the value b: '));

// addition and subtraction operator

a = a + b;

b = a - b;
a = a - b;

document.write("value of a :"+a);

document.write("<br>");

document.write("value of b :"+b);

</script>

Example
<script>

let text1 = prompt("Enter any number:");

let sqrt1=Math.sqrt(text1);

document.write("the squire root of text1: "+sqrt1);

</script>

JavaScript Comparison Operators


Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
JavaScript String Comparison
All the comparison operators above can also be used on strings:

Example
<script>

let x=10;

let y=20;

let result=x<y;

document.write("x is less then y :"+(x<y));

document.write("<br>");

document.write("x is greater then y :"+(x>y));

</script>

Example
<script>

let text1 = "A";

let text2 = "B";

let result = text1 < text2;

document.write(result);
</script>

Example

<script>

let text1 = "20";

let text2 = "5";

let result = text1 < text2;

document.write(result);

</script>

JavaScript String Addition


The + can also be used to add (concatenate) strings:

<script>

let text1 = "John";

let text2 = "Doe";

let text3 = text1 + " " + text2;

document.write(text3);

</script>

The += assignment operator can also be used to add (concatenate) strings:

<script>

let text1 = "What a very ";

text1 += "nice day";


document.write(text1);

</script>

Adding Strings and Numbers


Adding two numbers, will return the sum as a number like 5 + 5 = 10.

Adding a number and a string, will return the sum as a concatenated string like
5 + "5" = "55".

<script>
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;

document.write(x);

document.write(y);

document.write(z);

</scritp>

You might also like