Ad Code

Responsive Advertisement

JAVASCRIPT TUTORIAL FOR BEGINNER


JAVASCRIPT TUTORIAL FOR BEGINNER

Below is a comprehensive, step‑by‑step JavaScript tutorial—starting from the very basics and progressing through intermediate and advanced topics. Feel free to follow along in your browser’s developer console or in a code editor like VS Code.


1. Introduction to JavaScript

  • What is JavaScript?
    A high‑level, interpreted programming language that runs in browsers and on servers (via Node.js). It enables interactive web pages, server‑side scripting, desktop apps, and more.

  • How JavaScript Runs

    • Browser: Engines like V8 (Chrome), SpiderMonkey (Firefox), JavaScriptCore (Safari).

    • Server: Node.js uses V8 to execute JS outside the browser.


2. Setting Up Your Environment

  1. Browser Developer Tools

    • Open Chrome/Firefox → Right‑click → Inspect → Console tab.

  2. Text Editor

    • VS Code, Sublime Text, Atom, etc.

  3. Node.js (Optional)

    • Download from https://nodejs.org to run JS on your machine’s terminal:

      node --version
      

3. Basic Syntax & Concepts

3.1. Comments

// Single‑line comment

/*
  Multi‑line
  comment
*/

3.2. Variables

  • var (function‑scoped, legacy)

  • let (block‑scoped, preferred)

  • const (block‑scoped, read‑only)

let name = 'Alice';
const PI = 3.14159;

3.3. Data Types

  • Primitive: string, number, boolean, null, undefined, symbol, bigint

  • Object: arrays, functions, objects, dates, etc.

typeof 123;          // "number"
typeof 'hello';      // "string"
typeof null;         // "object"  (historic quirk)

4. Operators & Expressions

  • Arithmetic: +, -, *, /, %

  • Assignment: =, +=, -=

  • Comparison: ==, ===, !=, !==, <, >

  • Logical: &&, ||, !

  • Ternary:

    let msg = age >= 18 ? 'Adult' : 'Minor';
    

5. Control Flow

5.1. Conditional Statements

if (score > 90) {
  console.log('A');
} else if (score > 75) {
  console.log('B');
} else {
  console.log('C');
}

5.2. Switch

switch (day) {
  case 'Mon':
    //...
    break;
  default:
    //...
}

5.3. Loops

  • for:

    for (let i = 0; i < 5; i++) {
      console.log(i);
    }
    
  • while and do…while:

    let i = 0;
    while (i < 5) { /*…*/ i++; }
    

6. Functions

6.1. Function Declaration

function add(a, b) {
  return a + b;
}

6.2. Function Expression

const multiply = function(a, b) {
  return a * b;
};

6.3. Arrow Functions (ES6)

const square = x => x * x;
const sum = (a, b) => {
  const result = a + b;
  return result;
};

7. Objects & Arrays

7.1. Objects

const person = {
  name: 'Bob',
  age: 30,
  greet() {
    console.log(`Hi, I'm ${this.name}`);
  }
};

person.greet();  // “Hi, I’m Bob”

7.2. Arrays

const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.length);    // 3
fruits.push('kiwi');
fruits.forEach(fruit => console.log(fruit));

8. The DOM (Document Object Model)

8.1. Selecting Elements

const el = document.getElementById('myId');
const items = document.querySelectorAll('.item');

8.2. Manipulating Elements

el.textContent = 'Hello world!';
el.style.color = 'blue';

8.3. Event Handling

button.addEventListener('click', () => {
  alert('Clicked!');
});

9. Asynchronous JavaScript

9.1. Callbacks

setTimeout(() => {
  console.log('Delayed message');
}, 1000);

9.2. Promises

fetch('/api/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.error(err));

9.3. async / await (ES2017)

async function getData() {
  try {
    const resp = await fetch('/api/data');
    const json = await resp.json();
    console.log(json);
  } catch (err) {
    console.error(err);
  }
}
getData();

10. ES6+ Modern Features

  • Template Literals

    const msg = `Hello, ${name}!`;
    
  • Destructuring

    const [a, b] = [1, 2];
    const { x, y } = { x: 10, y: 20 };
    
  • Spread & Rest

    const nums = [1, 2, 3];
    const more = [...nums, 4, 5];
    function sumAll(...args) { /*…*/ }
    
  • Modules (Import / Export)

    // math.js
    export function add(a, b) { return a + b; }
    
    // main.js
    import { add } from './math.js';
    

11. Error Handling

try {
  // dangerous code
} catch (error) {
  console.error('Oops!', error);
} finally {
  console.log('Always runs');
}

12. Tooling & Best Practices

  • Linters: ESLint

  • Formatters: Prettier

  • Transpilation: Babel (for older browser support)

  • Bundlers: Webpack, Rollup, Parcel, Vite

Tips:

  • Use strict mode:

    'use strict';
    
  • Keep functions small and focused.

  • Favor const and let; avoid var.

  • Write meaningful names for variables and functions.

  • Comment complex logic, but keep code self‑documenting where possible.


13. Next Steps & Resources

  1. Deepen your understanding by building small projects:

    • To‑do list, calculator, weather app.

  2. Learn frameworks/libraries:

    • React, Vue, Angular, Svelte.

  3. Explore backend JS:

    • Node.js, Express, Koa.

  4. Official docs & tutorials:


With this foundation, you’re ready to write real‑world JavaScript. Happy coding!

Post a Comment

0 Comments