Docsity

Basic JavaScript for Beginners, Summaries of Computer Engineering and Programming

Toggle sidebar
bg1
JavaScript in a
JavaScript in a Nutshell
What is JavaScript?
· A high-level, interpreted programming language
· Mainly used for web development (front-end & back-end)
· Runs in browsers and on servers (Node.js)
· Adds interactivity to websites
Core Concepts
1. Variables & Data Types
```javascript
let name = "John"; // String
const age = 25; // Number (constant)
var isStudent = true; // Boolean
let scores = [95, 88, 72]; // Array
let person = { // Object
firstName: "Jane",
lastName: "Doe"
};
```
2. Basic Operations
```javascript
// Arithmetic
let sum = 5 + 3;
// String concatenation
let greeting = "Hello " + name;
// Comparison
let isAdult = age >= 18;
```
3. Functions
```javascript
function add(a, b) {
return a + b;
}
// Arrow function (modern)
const multiply = (x, y) => x * y;
```
4. Control Flow
```javascript
// If-else
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
// Loops
for (let i = 0; i < 5; i++) {
console.log(i);
pf3

Partial preview of the text

Download Basic JavaScript for Beginners and more Summaries Computer Engineering and Programming in PDF only on Docsity!

JavaScript in a

JavaScript in a Nutshell

What is JavaScript?

· A high-level, interpreted programming language · Mainly used for web development (front-end & back-end) · Runs in browsers and on servers (Node.js) · Adds interactivity to websites

Core Concepts

  1. Variables & Data Types
let name = "John"; // String const age = 25; // Number (constant) var isStudent = true; // Boolean let scores = [95, 88, 72]; // Array let person = { // Object firstName: "Jane", lastName: "Doe" }; 
  1. Basic Operations
// Arithmetic let sum = 5 + 3; // String concatenation let greeting = "Hello " + name; // Comparison let isAdult = age >= 18; 
  1. Functions
function add(a, b) { return a + b; } // Arrow function (modern) const multiply = (x, y) => x * y; 
  1. Control Flow
// If-else if (age >= 18) { console.log("Adult"); } else { console.log("Minor"); } // Loops for (let i = 0; i < 5; i++) { console.log(i); ## ``` 5. DOM Manipulation (Browser) ```javascript // Select element const button = document.getElementById("myButton"); // Change content button.textContent = "Click me!"; // Add event listener button.addEventListener("click", function() { alert("Button clicked!"); }); 

Key Features

· Event-driven: Responds to user actions · Asynchronous: Can handle multiple operations without blocking · Object-oriented: Uses objects and classes · Dynamic typing: Variable types determined at runtime

Common Use Cases

  1. Form validation
  2. Dynamic content updates
  3. API calls (fetch data from servers)
  4. Animations and effects
  5. Single Page Applications (SPA)

Modern JavaScript (ES6+)

// Template literals let message = `Hello ${name}, you are ${age} years old`; // Destructuring let [first, second] = scores; let { firstName, lastName } = person; // Promises & Async/Await async function fetchData() { let response = await fetch("url"); let data = await response.json(); return data; } 

Quick Tips

· Use console.log() for debugging · Modern code uses let and const instead of var · JavaScript is case-sensitive · Semicolons are optional but recommended

Learning Path

  1. Basic syntax & DOM manipulation
  2. ES6+ features