JAVA SCRIPT Full Course For Beginners Lesson No 2.0

Haroon Blogger
0

JAVA SCRIPT Full Course For Beginners Lesson No 2.0

 

 

Lesson 2: JavaScript Variables, Data Types, and Operations


 

Variables in JavaScript


In JavaScript, you can declare variables using `var`, `let`, or `const`. Here's a detailed breakdown:

 `var`:

- The older way of declaring variables.


- It has a function-scoped scope.


- It is less commonly used now due to potential issues with scoping.

JavaScript
```javascript
var x = 5;
```



 `let`:

- Introduced in ECMAScript 6 (ES6) and is the modern way for mutable variables.
- It has a block-scoped scope.
- Use `let` when the variable may be reassigned.

JavaScript
```javascript
let message = "Hello, JavaScript!";
```



 `const`:

- Also introduced in ES6.
- It creates a constant variable that cannot be reassigned.
- It has a block-scoped scope.
- Use `const` when the value should not change.

JavaScript
```javascript
const pi = 3.14;
```



 

Data Types in JavaScript


 1. Number:

- Represents numeric values (integers or floating-point numbers).

JavaScript
```javascript
let numberVar = 42;
let floatVar = 3.14;
```


 2. String:

- Represents text and can be enclosed in single or double quotes.

JavaScript
```javascript
let stringVar = "JavaScript";
let anotherString = 'Web Development';
```

 3. Boolean:

- Represents logical values, either `true` or `false`.

JavaScript
```javascript
let isTrue = true;
let isFalse = false;
```



 4. Array:

- Represents an ordered list of values. Arrays can hold values of different types.

JavaScript
```javascript
let numbers = [1, 2, 3, 4];
let mixedArray = [1, "two", true];
```



 5. Object:

- Represents a collection of key-value pairs.

JavaScript
```javascript
let person = {
    firstName: "John",
    lastName: "Doe",
    age: 30,
    isStudent: false
};
```



 

Basic Operations in JavaScript


 1. Arithmetic Operations:

- Perform mathematical operations.

JavaScript
```javascript
let sum = 5 + 3;       // 8
let product = 6  7;    // 42
let quotient = 20 / 4;  // 5
let remainder = 15 % 4; // 3
```



 2. String Concatenation:

- Combine strings using the `+` operator.

JavaScript
```javascript
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName; // "John Doe"
```



 3. Comparison Operators:

- Compare values and return a Boolean result.

JavaScript
```javascript
let isEqual = 5 === "5";       // false
let isNotEqual = 10 !== 5;      // true
let greaterThan = 8 > 3;        // true
let lessThanOrEqual = 4 <= 4;   // true
```



 4. Logical Operators:

- Combine or modify Boolean values.

JavaScript
```javascript
let logicalAnd = true && false; // false
let logicalOr = true || false;  // true
let logicalNot = !true;         // false
```



 Example: Using Variables and Operations


JavaScript
```javascript
let baseSalary = 50000;
let bonus = 10000;
let totalSalary = baseSalary + bonus;

let welcomeMessage = "Welcome, " + "John!";
let isEligible = totalSalary > 60000 && totalSalary <= 70000;
```


Exercise


1. Practice with Variables:

   - Declare variables of different data types.


   - Experiment with `var`, `let`, and `const`.

2. Explore Data Types:

   - Create arrays with different types of elements.


   - Build objects with various key-value pairs.

3. Master Operations:

   - Perform arithmetic operations with different numbers.


   - Concatenate strings in various ways.


   - Use comparison and logical operators in different scenarios.


Post a Comment

0Comments

Post a Comment (0)
To Top