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

javascript

Uploaded by

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

javascript

Uploaded by

bakhtawark085
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

// // /*

// // Operators in JavaScript

// // Operators are symbols or keywords used to perform operations


// // on values (operands). They can be categorized as follows:

// // 1=Arithmetic Operators
// // 2=Assignment Operators
// // 3=Comparison Operators
// // 4=Logical Operators
// // 5=Unary Operators
// // 6=Ternary Operator
// // 7=Bitwise Operators
// // 8=String Operators
// // 9=Type Operators
// // 10=Comma Operator

// // 1. Arithmetic Operators
// // These operators are used for basic mathematical calculations.

// // + (Addition): Adds two numbers or concatenates strings.


// // Example:
// // 5 + 3 → 8
// // "Hello" + " World" → "Hello World"

// // - (Subtraction): Subtracts one number from another.


// // Example:
// // 10 - 4 → 6

// // * (Multiplication): Multiplies two numbers.


// // Example:
// // 6 * 7 → 42

// // / (Division): Divides one number by another.


// // Example:
// // 20 / 5 → 4

// // ** (Exponentiation): Raises the first number to the power of the second.


// // Example:
// // 2 ** 3 → 8

// // % (Modulus): Returns the remainder after division.


// // Example:
// // 10 % 3 → 1

// // 2. Assignment Operators
// // These operators are used to assign values to variables and modify their
values.
// // = (Assign): Assigns a value to a variable.
// // Example:
// // let x = 5;

// // += (Add and Assign): Adds a value to the variable and assigns the
result.
// // Example:
// // x += 3; // x = x + 3

// // -= (Subtract and Assign): Subtracts a value from the variable and


assigns the result.
// // Example:
// // x -= 2; // x = x - 2

// // *= (Multiply and Assign): Multiplies a value with the variable and


assigns the result.
// // Example:
// // x *= 2; // x = x * 2

// // /= (Divide and Assign): Divides a value by the variable and assigns the
result.
// // Example:
// // x /= 2; // x = x / 2

// // %= (Modulus and Assign): Finds the remainder and assigns the result.
// // Example:
// // x %= 3; // x = x % 3

// // **= (Exponentiate and Assign): Raises the variable to the power and
assigns the result.
// // Example:
// // x **= 2; // x = x ** 2

// // Bitwise Assignment Operators:


// // &= (AND), |= (OR), ^= (XOR) perform bitwise operations and assign the
result to the variable.
// // Example:
// // x &= 3; // bitwise AND

// // 3. Comparison Operators
// // These operators compare two values and return true or false.

// // == (Equal): Checks if two values are equal, ignoring their types.


// // Example:
// // 5 == "5" → true

// // === (Strict Equal): Checks if two values are equal and of the same type.
// // Example:
// // 5 === "5" → false

// // != (Not Equal): Checks if two values are not equal.


// // Example:
// // 5 != "5" → false

// // !== (Strict Not Equal): Checks if two values are not equal or have
different types.
// // Example:
// // 5 !== "5" → true

// // > (Greater Than): Checks if the left value is greater than the right.
// // Example:
// // 7 > 5 → true

// // < (Less Than): Checks if the left value is less than the right.
// // Example:
// // 3 < 5 → true

// // >= (Greater Than or Equal): Checks if the left value is greater than or
equal to the right.
// // Example:
// // 7 >= 7 → true

// // <= (Less Than or Equal): Checks if the left value is less than or equal
to the right.
// // Example:
// // 4 <= 5 → true

// // 4. Logical Operators
// // These operators are used to combine boolean expressions.

// // && (AND): Returns true if both conditions are true.


// // Example:
// // true && true → true

// // || (OR): Returns true if at least one condition is true.


// // Example:
// // true || false → true

// // ! (NOT): Reverses the boolean value.


// // Example:
// // !true → false

// // 5. Unary Operators
// // These operators apply to a single operand.
// // ++ (Increment): Increases the operand's value by 1.
// // Example:
// // x++ (post-increment), ++x (pre-increment)

// // -- (Decrement): Decreases the operand's value by 1.


// // Example:
// // x-- (post-decrement), --x (pre-decrement)

// // 6. Ternary Operator
// // A concise way to write an if-else statement.

// // Syntax: condition ? true_value : false_value


// // Example:
// // age >= 18 ? "Yes" : "No"

// // If the condition is true, it returns the first value; otherwise, the


second value.

// // 7. Bitwise Operators
// // These operators work on the binary form of numbers.

// // & (AND): Returns 1 if both bits are 1.


// // Example:
// // 5 & 3 → 1 (Binary: 0101 & 0011 = 0001)

// // | (OR): Returns 1 if at least one bit is 1.


// // Example:
// // 5 | 3 → 7 (Binary: 0101 | 0011 = 0111)

// // ^ (XOR): Returns 1 if the bits are different.


// // Example:
// // 5 ^ 3 → 6 (Binary: 0101 ^ 0011 = 0110)

// // ~ (NOT): Inverts all bits.


// // Example:
// // ~5 → -6 (Binary: 0101 → 1010)

// // << (Left Shift): Shifts bits to the left by the specified number of
positions.
// // Example:
// // 5 << 1 → 10 (Binary: 0101 << 1 = 1010)

// // >> (Right Shift): Shifts bits to the right by the specified number of
positions.
// // Example:
// // 5 >> 1 → 2 (Binary: 0101 >> 1 = 0010)
// // >>> (Unsigned Right Shift): Shifts bits to the right, filling with 0
from the left.
// // Example:
// // -5 >>> 1 → 2147483642

// // 8. String Operators
// // These operators are used to manipulate strings.

// // + (Concatenation): Combines two or more strings into one.


// // Example:
// // "Hello" + " World" → "Hello World"

// // += (Concatenation Assignment): Adds a string to an existing string.


// // Example:
// // str += " World"; // str = "Hello World"

// // 9. Type Operators
// // These operators check or determine the type of a value.

// // typeof: Returns the type of a value.


// // Example:
// // typeof "Hello" → "string"

// // instanceof: Checks if an object is an instance of a particular class.


// // Example:
// // "Hello" instanceof String → false,
// // [] instanceof Array → true

// // 10. Comma Operator


// // This operator evaluates multiple expressions and returns the result of
the last expression.

// // Syntax: expr1, expr2, expr3


// // Example:
// // let x = (1, 2, 3); // x = 3

// // Unary operators:
// // ++ (Increment): Increases a variable's value by 1.
// // -- (Decrement): Decreases a variable's value by 1.
// // c++ = (post Increment) ; ++c = (pre Increment);
// // d-- = (post Decrement) ; --c = (pre Decrement);
// // */

// // // 1. Arithmetic Operators
// // let a = 5;
// // let b = 10;

// // console.log("a =", a, "b =", b); // Output: a = 5 b = 10


// // console.log("a + b =", a + b); // Output: a + b = 15 (Addition)
// // console.log("a - b =", a - b); // Output: a - b = -5 (Subtraction)
// // console.log("a * b =", a * b); // Output: a * b = 50 (Multiplication)
// // console.log("a ** b =", a ** b); // Output: a ** b = 9765625
(Exponentiation)
// // console.log("a / b =", a / b); // Output: a / b = 0.5 (Division)
// // console.log("a % b =", a % b); // Output: a % b = 5 (Modulus)

// // // 2. Assignment Operators
// // let x = 5;

// // x += 3; // x = x + 3 -> 8
// // console.log("x += 3 →", x);

// // x -= 2; // x = x - 2 -> 6
// // console.log("x -= 2 →", x);

// // x *= 2; // x = x * 2 -> 12
// // console.log("x *= 2 →", x);

// // x /= 2; // x = x / 2 -> 6
// // console.log("x /= 2 →", x);

// // x %= 3; // x = x % 3 -> 0
// // console.log("x %= 3 →", x);

// // x **= 2; // x = x ** 2 -> 0
// // console.log("x **= 2 →", x);

// // // 3. Comparison Operators
// // console.log("5 == '5' →", 5 == '5'); // Output: true (Loose equality)
// // console.log("5 === '5' →", 5 === '5'); // Output: false (Strict
equality)
// // console.log("5 != '5' →", 5 != '5'); // Output: false (Loose inequality)
// // console.log("5 !== '5' →", 5 !== '5'); // Output: true (Strict
inequality)
// // console.log("7 > 5 →", 7 > 5); // Output: true (Greater than)
// // console.log("3 < 5 →", 3 < 5); // Output: true (Less than)
// // console.log("7 >= 7 →", 7 >= 7); // Output: true (Greater than or equal)
// // console.log("4 <= 5 →", 4 <= 5); // Output: true (Less than or equal)

// // // 4. Logical Operators
// // console.log("true && true →", true && true); // Output: true (AND)
// // console.log("true || false →", true || false); // Output: true (OR)
// // console.log("!true →", !true); // Output: false (NOT)

// // // 5. Unary Operators
// // let c = 5;
// // console.log("c++ →", c++); // Post-increment
// // console.log("++c →", ++c); // Pre-increment

// // let d = 10;
// // console.log("d-- →", d--); // Post-decrement
// // console.log("--d →", --d); // Pre-decrement

// // // 6. Ternary Operator
// // let age = 20;
// // console.log("age >= 18 ? 'Yes' : 'No' →", age >= 18 ? "Yes" : "No"); //
Output: Yes

// // // 7. Bitwise Operators
// // console.log("5 & 3 →", 5 & 3); // Output: 1 (Binary: 0101 & 0011 = 0001)
// // console.log("5 | 3 →", 5 | 3); // Output: 7 (Binary: 0101 | 0011 = 0111)
// // console.log("5 ^ 3 →", 5 ^ 3); // Output: 6 (Binary: 0101 ^ 0011 = 0110)
// // console.log("~5 →", ~5); // Output: -6 (Binary: ~0101 = 1010)
// // console.log("5 << 1 →", 5 << 1); // Output: 10 (Binary: 0101 << 1 =
1010)
// // console.log("5 >> 1 →", 5 >> 1); // Output: 2 (Binary: 0101 >> 1 = 0010)
// // console.log("-5 >>> 1 →", -5 >>> 1); // Output: 2147483642 (Unsigned
right shift)

// // // 8. String Operators
// // console.log('"Hello" + " World" →', "Hello" + " World"); // Output:
"Hello World"
// // let str = "Hello";
// // str += " World"; // Concatenation assignment
// // console.log("str += ' World' →", str); // Output: "Hello World"

// // // 9. Type Operators
// // console.log('typeof "Hello" →', typeof "Hello"); // Output: "string"
// // console.log('[] instanceof Array →', [] instanceof Array); // Output:
true
// // console.log('"Hello" instanceof String →', "Hello" instanceof String);
// Output: false

// // // 10. Comma Operator


// // let xComma = (1, 2, 3);
// // console.log("let xComma = (1, 2, 3); →", xComma); // Output: 3

// // /arthmetic operator

// // let a = 5;
// // let b = 2;
// // console.log(a+b);
// // console.log(a-b);
// // console.log(a*b);
// // console.log(a/b);
// // console.log(a%b); //modulus
// // console.log(a**b); //Exponentiation
// // //unary operators
// // console.log(a++);
// // console.log(++a);
// // console.log(a--);
// // console.log(--a);

// // assigment operators
// // let a = 5;
// // let b = 5;
// // a = a+5;
// // a+=3;
// // a-=1;

// // comparison operators

// // let a =55;
// // let b=45;
// // console.log(a==b);
// // let name = prompt("enter your name")
// // let userName = "bakhtawar";
// // alert(name==userName);
// // let password = prompt("enter a password");
// // let savePassword ="pakistane123";
// // alert(password==savePassword);

// // let a = "bryan";
// // let b = "bryani";
// // console.log(a!=b);

// // let a = "65";
// // let b = 45;
// // console.log(a>=b);

// // logical oprators
// // let a = 8 ;
// // let b= 4;
// // let cond1 = a>b;
// // let cond2 = a===5;
// // console.log("cond1 && cond2 =", cond1 && cond2);
// // console.log("cond1 || cond2 =", cond1 || cond2);
// // console.log("cond1 ! cond2 =", !(a===8));
// // let age = prompt("Enter your age ");
// // if (age==20){
// // alert("you are eligble for vote ")}
// // else{
// // alert("you are not eligible")
// // }

// // let myName = "muhammad";


// // if (myName=="muhammad"){
// // alert("welcome", myName);
// // }
// // else {
// // alert("your record is not avilable please sing in ");

// // }

// // let name = prompt("Enter your name");

// // if (name && name) {


// // alert(`Hi Dear ${name}`);
// // } else {
// // alert("Bye, Mr.");
// // }

// // let myName = prompt("Enter your name");


// // if (myName=="muhammad") {
// // alert(`Hi Dear Mr. ${myName}`);
// // } else {
// // alert("You didn't enter your name.");
// // }

// // let age = prompt("Enter your age: ");


// // age = parseInt(age); // Convert input to a number

// // if (age >= 50) {

// // alert("Shadi burhiya se karni paregi (50 years).");


// // } else if (age >= 35) {
// // alert("Shadi anty se karni paregi (35 years).");
// // } else if (age >= 25) {
// // alert("Shadi 25 saal ki larki se karni paregi.");
// // } else {
// // alert("Barwa abhi school par focus karo.");
// // }
// // let age = prompt("Enter your age ");
// // if(age>=18){
// // alert("you can vote");
// // } else{
// // alert("you can not vote");
// // }

// // let mode = prompt("Enter a mode");


// // let color;
// // if (mode==="dark"){
// // alert(color="black")
// // }
// // if (mode==="light"){
// // alert(color="whithe")
// // }

// // let num = prompt("Enter a number");


// // num=Number(num);

// // if (num % 2 ===0){
// // alert(num + " is Even")
// // }
// // else {
// // alert(num + " is odd")
// // }

// // junior
// // senior
// // middle

// // let age= prompt("Enter Your age ");


// // age=Number(age);
// // if (age>=50){
// // alert("senior");
// // }
// // else if (age>=18){
// // alert("junior");
// // }
// // else
// // {
// // alert("middle")
// // }

// // let Grade =prompt("Enter Your Marks ");


// // Grade=Number(Grade);
// // if (Grade>=90){
// // alert ("You Recived A Grade ")
// // }
// // else if (Grade>=80){
// // alert ("You Recived B Grade ")

// // }
// // else if(Grade>=70){
// // alert ("You Recived c Grade ")

// // }
// // else if (Grade>=50){
// // alert ("You Recived D Grade ")

// // }
// // else if (Grade>=40){
// // alert ("You Recived E Grade ")

// // }
// // else{
// // alert ("You are Fail ")

// // }

// // let shop = prompt("Enter a Product name ");


// // if (shop=="doodh"){
// // alert("price=230kg")
// // }
// // else if (shop=="dahia"){
// // alert("price=290kg")
// // }
// // else if (shop=="bakridoodh"){
// // alert("price=490kg")
// // }
// // else if (shop=="bun"){
// // alert("price=40kg")
// // }
// // else{
// // alert("wapsi aja");
// // }

// let shop = prompt("Enter a Product name ");


// if (shop === "doodh") {
// alert("Price: 230/kg");
// } else if (shop === "dahia") {
// alert("Price: 290/kg");
// } else if (shop === "bakridoodh") {
// alert("Price: 490/kg");
// } else if (shop === "bun") {
// alert("Price: 40/kg");
// } else if (shop === "bread") {
// alert("Price: 50/kg");
// } else if (shop === "cake") {
// alert("Price: 500/kg");
// } else if (shop === "pastry") {
// alert("Price: 100/kg");
// } else if (shop === "cookies") {
// alert("Price: 300/kg");
// } else if (shop === "croissant") {
// alert("Price: 80/kg");
// } else if (shop === "baguette") {
// alert("Price: 120/kg");
// } else if (shop === "muffin") {
// alert("Price: 60/kg");
// } else if (shop === "pita") {
// alert("Price: 70/kg");
// } else if (shop === "roti") {
// alert("Price: 10/pc");
// } else if (shop === "paratha") {
// alert("Price: 15/pc");
// } else if (shop === "samosa") {
// alert("Price: 20/pc");
// } else if (shop === "biryani") {
// alert("Price: 300/kg");
// } else if (shop === "karahi") {
// alert("Price: 500/kg");
// } else if (shop === "dal") {
// alert("Price: 150/kg");
// } else if (shop === "chawal") {
// alert("Price: 120/kg");
// } else if (shop === "chai") {
// alert("Price: 50/packet");
// } else if (shop === "sugar") {
// alert("Price: 100/kg");
// } else if (shop === "salt") {
// alert("Price: 20/kg");
// } else {
// alert("Wapsi aja");
// }
// let age = 12;
// let result = age>=18 ? "Adult":"Not adult";
// alert(result);
// let age = prompt("Enter your age");
// age = Number(age);
// alert(age >= 18 ? "adult" : "not adult")

// let price = 20;


// alert (++price);
// alert (--price);
// alert (price++);
// alert(price);

// alert ("10"==10);
// alert("10"===10);
// alert ("10"!=5);

// let age = prompt("Enter your age ")


// alert((age>=18)? "you can vote" :"you can not vote");

// let item = prompt("Enter Item Name");


// alert((item == "bryani" )?"per plate Bryani Rs:200":"No item Avilable");

// let item = prompt("Enter Item Name")


// let price;

// switch (item) {
// case "biryani":
// price = "Per plate Biryani Rs: 200";
// break;
// case "karahi":
// price = "Per plate Karahi Rs: 500";
// break;
// case "nihari":
// price = "Per plate Nihari Rs: 300";
// break;
// case "kabab":
// price = "Per plate Kabab Rs: 150";
// break;
// case "haleem":
// price = "Per plate Haleem Rs: 250";
// break;
// default:
// price = "No item available";
// }
// alert(price);

// let age = prompt("Enter Your age ");


// alert(( age>18 ?"You can vote " :"You can no vote"));

You might also like