05 Introduct with operator

Here are five examples of JavaScript operators, starting from easy to hard. Each example outputs the result directly to the browser using `document.write` for simplicity.


 

---


 

### 1. **Arithmetic Operator (+)** - Easy
```javascript
document.write("<h3>Arithmetic Operator (+)</h3>");
let a = 5;
let b = 10;
document.write("Sum of a and b is: " + (a + b));
```


 

---


 

### 2. **Comparison Operator (==)** - Easy
```javascript
document.write("<h3>Comparison Operator (==)</h3>");
let x = 10;
let y = "10";
document.write("Is x equal to y? " + (x == y)); // true
```


 

---


 

### 3. **Logical Operator (&&)** - Medium
```javascript
document.write("<h3>Logical Operator (&&)</h3>");
let age = 25;
let hasLicense = true;
document.write("Can drive? " + (age >= 18 && hasLicense)); // true
```


 

---


 

### 4. **Ternary Operator (condition ? expr1 : expr2)** - Medium
```javascript
document.write("<h3>Ternary Operator</h3>");
let marks = 75;
document.write(marks >= 50 ? "Passed" : "Failed");
```


 

---


 

### 5. **Bitwise Operator (&)** - Hard
```javascript
document.write("<h3>Bitwise Operator (&)</h3>");
let num1 = 5;  // Binary: 0101
let num2 = 3;  // Binary: 0011
document.write("Bitwise AND of num1 and num2 is: " + (num1 & num2)); // Binary result: 0001 (Decimal: 1)
```


 

---


 

Copy and paste any of these examples into an HTML file to see them displayed in your browser! 😊
2 min read
Nov 19, 2024
By Md Real
Share

Leave a comment

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