🚀 𝗔𝗜 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗖𝗼𝘂𝗿𝘀𝗲 😍| 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮𝗻 𝗔𝗜 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿
This AI engineering course online is structured around AI fundamentals, full-stack development, real-world AI projects
💫2000+ Students Placed
🎥 Live Classes + Session Recordings
👨💻 1:1 Mentorship & Doubt Support
💼 Interview Preparation & Placement Assistance
𝗕𝗼𝗼𝗸 𝗮 𝗙𝗥𝗘𝗘 𝗗𝗲𝗺𝗼👇:-
https://pdlink.in/4fWJVID
🔥 Take the next step towards building your career in AI!
This AI engineering course online is structured around AI fundamentals, full-stack development, real-world AI projects
💫2000+ Students Placed
🎥 Live Classes + Session Recordings
👨💻 1:1 Mentorship & Doubt Support
💼 Interview Preparation & Placement Assistance
𝗕𝗼𝗼𝗸 𝗮 𝗙𝗥𝗘𝗘 𝗗𝗲𝗺𝗼👇:-
https://pdlink.in/4fWJVID
🔥 Take the next step towards building your career in AI!
❤1
💻 JavaScript Learning Series, Day 2
Topic 2: Variables & Data Types in JavaScript
Today, we'll learn how to store information in JavaScript and understand the different types of data we can work with.
1. What Are Variables?
A variable is a named container used to store data in a program.
For example:
Output:
Here:
• name stores a string.
• age stores a number.
• salary stores a number.
2. How to Declare Variables
JavaScript provides three main ways to declare variables.
A. var
• Function-scoped.
• Can be redeclared.
• Can be reassigned.
• Generally avoided in modern JavaScript.
B. let
• Block-scoped.
• Cannot be redeclared in the same scope.
• Can be reassigned.
Output: 30
C. const
• Block-scoped.
• Cannot be redeclared in the same scope.
• Cannot be reassigned.
Output: India
Remember: A const object or array can still have its contents modified. The variable binding itself cannot be reassigned.
3. Difference Between var, let & const
Best Practice: Use const by default and let when reassignment is required. Avoid var in modern JavaScript.
4. JavaScript Data Types
JavaScript has two broad categories of data types.
A. Primitive Data Types
There are seven primitive types.
1. String
Used to store text.
2. Number
Used for integers and decimal values.
3. Boolean
Stores either true or false.
4. Undefined
A variable that has been declared but not assigned a value.
Output: undefined
5. Null
Represents an intentional absence of a value.
6. BigInt
Used for integers larger than the safe integer limit of the Number type.
7. Symbol
Creates unique identifiers.
B. Non-Primitive Data Types
These are commonly represented using objects.
1. Object
Stores data in key-value pairs.
2. Array
Stores multiple values in an ordered collection.
3. Function
A reusable block of code.
5. Checking Data Types Using typeof
JavaScript provides the typeof operator to identify the type of a value.
Important:
6. Dynamic Typing in JavaScript
JavaScript is dynamically typed. This means a variable can hold different types of values during execution.
The same variable first stores a number and then stores a string.
7. Type Conversion
Type conversion means converting one data type into another.
String to Number
Topic 2: Variables & Data Types in JavaScript
Today, we'll learn how to store information in JavaScript and understand the different types of data we can work with.
1. What Are Variables?
A variable is a named container used to store data in a program.
For example:
let name = "John";
let age = 25;
let salary = 45000;
console.log(name);
console.log(age);
console.log(salary);
Output:
John
25
45000
Here:
• name stores a string.
• age stores a number.
• salary stores a number.
2. How to Declare Variables
JavaScript provides three main ways to declare variables.
A. var
• Function-scoped.
• Can be redeclared.
• Can be reassigned.
• Generally avoided in modern JavaScript.
var city = "Mumbai";
var city = "Pune";
console.log(city);
B. let
• Block-scoped.
• Cannot be redeclared in the same scope.
• Can be reassigned.
let age = 25;
age = 30;
console.log(age);
Output: 30
C. const
• Block-scoped.
• Cannot be redeclared in the same scope.
• Cannot be reassigned.
const country = "India";
console.log(country);
Output: India
Remember: A const object or array can still have its contents modified. The variable binding itself cannot be reassigned.
3. Difference Between var, let & const
Feature: Scope
var: Function
let: Block
const: Block
Feature: Reassignment
var: Yes
let: Yes
const: No
Feature: Redeclaration
var: Yes
let: No
const: No
Feature: Hoisting
var: Yes
let: Yes
const: Yes
Feature: Initialization required
var: No
let: No
const: Yes
Best Practice: Use const by default and let when reassignment is required. Avoid var in modern JavaScript.
4. JavaScript Data Types
JavaScript has two broad categories of data types.
A. Primitive Data Types
There are seven primitive types.
1. String
Used to store text.
let name = "Rahul";
2. Number
Used for integers and decimal values.
let age = 25;
let price = 99.99;
3. Boolean
Stores either true or false.
let isActive = true;
let isCompleted = false;
4. Undefined
A variable that has been declared but not assigned a value.
let result;
console.log(result);
Output: undefined
5. Null
Represents an intentional absence of a value.
let selectedUser = null;
6. BigInt
Used for integers larger than the safe integer limit of the Number type.
let bigNumber = 12345678901234567890n;
7. Symbol
Creates unique identifiers.
let id = Symbol("user");
B. Non-Primitive Data Types
These are commonly represented using objects.
1. Object
Stores data in key-value pairs.
let student = {
name: "Rahul",
age: 22,
city: "Delhi"
};
2. Array
Stores multiple values in an ordered collection.
let fruits = ["Apple", "Mango", "Banana"];
3. Function
A reusable block of code.
function greet() {
console.log("Hello!");
}
5. Checking Data Types Using typeof
JavaScript provides the typeof operator to identify the type of a value.
console.log(typeof "Hello");
console.log(typeof 100);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 100n);
console.log(typeof Symbol("id"));
console.log(typeof {});
string
number
boolean
undefined
bigint
symbol
object
Important:
typeof null also returns "object". This is a historical JavaScript behavior.6. Dynamic Typing in JavaScript
JavaScript is dynamically typed. This means a variable can hold different types of values during execution.
let data = 100;
console.log(data);
data = "Hello JavaScript";
console.log(data);
100
Hello JavaScript
The same variable first stores a number and then stores a string.
7. Type Conversion
Type conversion means converting one data type into another.
String to Number
❤3
let value = "100";
let result = Number(value);
Output: 100
Number to String
let age = 25;
let result = String(age);
Output:
25 (as a string)String to Boolean
let value = "Hello";
console.log(Boolean(value));
Output: true
8. Variable Naming Rules
Follow these rules when naming variables:
- Names can contain letters, numbers,
_ and $.- Names cannot start with a number.
- Variable names are case-sensitive.
- Reserved keywords cannot be used as variable names.
- Use meaningful names.
Good examples:
let firstName = "Rahul";
let totalPrice = 500;
let isLoggedIn = true;
Invalid examples:
let 1name = "Rahul";
let let = 100;
Double Tap ❤️ For Part-3
❤7