0% found this document useful (0 votes)
7 views6 pages

JavaScript Basics: Variables to Functions

The document covers JavaScript fundamentals, including variables, data types, operators, conditions, loops, functions, objects, arrays, comparison and logical operators, events, DOM manipulation, Fetch API, error handling, and timing functions. It provides examples for each topic to illustrate their usage. This serves as a comprehensive guide for beginners to understand the core concepts of JavaScript.

Uploaded by

jeyasuthan265
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)
7 views6 pages

JavaScript Basics: Variables to Functions

The document covers JavaScript fundamentals, including variables, data types, operators, conditions, loops, functions, objects, arrays, comparison and logical operators, events, DOM manipulation, Fetch API, error handling, and timing functions. It provides examples for each topic to illustrate their usage. This serves as a comprehensive guide for beginners to understand the core concepts of JavaScript.

Uploaded by

jeyasuthan265
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

JavaScript fundamentals

🧠 1. Variables
Variables store data.

// Old way
var name = "Alice";

// Modern way
let age = 25;
const country = "USA"; // constant value

🔢 2. Data Types
let name = "Bob"; // String
let age = 30; // Number
let isHappy = true; // Boolean
let score = null; // Null
let x; // Undefined
let person = { name: "Tom", age: 20 }; // Object
let colors = ["red", "blue", "green"]; // Array

🧮 3. Operators
let x = 10;

JavaScript fundamentals 1
let y = 5;

[Link](x + y); // 15
[Link](x - y); // 5
[Link](x * y); // 50
[Link](x / y); // 2
[Link](x % y); // 0

🧾 4. Conditions ( if , else , else if )

let age = 18;

if (age >= 18) {


[Link]("You're an adult");
} else {
[Link]("You're a minor");
}

🔁 5. Loops ( for , while )

// For loop
for (let i = 0; i < 5; i++) {
[Link]("Count:", i);
}

// While loop
let i = 0;
while (i < 5) {
[Link]("While:", i);

JavaScript fundamentals 2
i++;
}

📦 6. Functions
function greet(name) {
return "Hello, " + name;
}

[Link](greet("Alice"));

Arrow function (modern syntax):

const greet = (name) => "Hi, " + name;

👤 7. Objects
const person = {
name: "John",
age: 30,
greet: function () {
return "Hi, I'm " + [Link];
}
};

JavaScript fundamentals 3
[Link]([Link]());

📚 8. Arrays
let fruits = ["apple", "banana", "cherry"];

[Link](fruits[0]); // apple
[Link]("orange"); // Add to end
[Link](); // Remove last

Loop through array:

[Link](fruit => {
[Link](fruit);
});

🧪 9. Comparison & Logical Operators


[Link](5 == "5"); // true (loose)
[Link](5 === "5"); // false (strict)
[Link](5 != 4); // true
[Link](true && false); // false
[Link](true || false); // true

📢 10. Events (in browser)


JavaScript fundamentals 4
<button onclick="sayHello()">Click Me</button>

<script>
function sayHello() {
alert("Hello!");
}
</script>

🧰 11. DOM Manipulation


<p id="demo">Hello</p>
<button onclick="changeText()">Change</button>

<script>
function changeText() {
[Link]("demo").innerText = "Changed!";
}
</script>

📬 12. Fetch API (Get data from web)


fetch('[Link]
.then(response => [Link]())
.then(data => [Link](data));

🧠
JavaScript fundamentals 5
🧠 13. Try / Catch (Error handling)
try {
let result = 5 / 0;
[Link](result);
} catch (error) {
[Link]("Error:", error);
}

🔁 14. SetInterval & SetTimeout


setTimeout(() => {
[Link]("Runs after 2 seconds");
}, 2000);

setInterval(() => {
[Link]("Repeats every 1 second");
}, 1000);

JavaScript fundamentals 6

You might also like