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

JavaScript_Basics_With_Code

The document provides an overview of JavaScript basics, covering variables, data types, operators, control structures, functions, objects, arrays, DOM manipulation, and events. It includes examples of how to declare variables, use different data types, and implement control structures like if/else statements and loops. Additionally, it demonstrates how to manipulate the DOM and handle events in JavaScript.
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)
171 views

JavaScript_Basics_With_Code

The document provides an overview of JavaScript basics, covering variables, data types, operators, control structures, functions, objects, arrays, DOM manipulation, and events. It includes examples of how to declare variables, use different data types, and implement control structures like if/else statements and loops. Additionally, it demonstrates how to manipulate the DOM and handle events in JavaScript.
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/ 4

JavaScript Basics

1. Variables

Used to store data.

let name = "Alice"; // can change later

const age = 25; // constant, cannot be changed

var city = "Paris"; // older way (not recommended)

2. Data Types

// String

"Hello"

// Number

10, 3.14

// Boolean

true, false

// Null

null

// Undefined

undefined

// Object
JavaScript Basics

{ key: "value" }

// Array

[1, 2, 3]

3. Operators

// Arithmetic

+-*/%

// Comparison

== === != !==

// Logical

&& || !

// Assignment

= += -=

4. Control Structures

// If/Else

if (age >= 18) {

console.log("Adult");

} else {

console.log("Minor");
JavaScript Basics

// For Loop

for (let i = 0; i < 5; i++) {

console.log(i);

// While Loop

while (condition) {

// do something

5. Functions

function greet(name) {

return "Hello " + name;

const greetArrow = (name) => "Hello " + name;

6. Objects & Arrays

// Object

let person = { name: "Alice", age: 25 };

console.log(person.name);
JavaScript Basics

// Array

let fruits = ["apple", "banana"];

console.log(fruits[0]);

7. DOM Manipulation

document.getElementById("myDiv").innerText = "Hello!";

8. Events

document.getElementById("btn").addEventListener("click", function() {

alert("Button clicked!");

});

You might also like