Skip to content

TheDevPath/Javascript-Crash-Course

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript Crash Course

Welcome to the JavaScript Crash Course! This repository contains a 60-minute crash course designed to teach you the essentials of JavaScript, from variables to building a functional to-do list app. Whether you're a beginner or refreshing your skills, this guide and the accompanying code will get you coding fast.

This README serves as the written guide, covering core JavaScript concepts in a concise format. The repository includes the code for a to-do list project demonstrated in the course. For a video version, check [insert video link here, or remove this sentence if no video is hosted].

Repository Contents

  • index.html: The HTML file for the to-do list project.
  • script.js: The JavaScript code for the to-do list functionality.
  • README.md: This file, containing the written crash course.
  • style.css: Optional CSS for styling the to-do list (if applicable).

Getting Started

  1. Clone this repository: git clone https://github.com/TheDevPath/Javascript-Crash-Course
  2. Open index.html in a browser to try the to-do list app.
  3. Follow the guide below to learn JavaScript essentials.

JavaScript Crash Course: Learn the Essentials in 60 Minutes

JavaScript is a versatile, high-level programming language used primarily for web development. This crash course covers the core concepts you need to start coding in JavaScript effectively.

1. What is JavaScript?

  • JavaScript runs in browsers to make websites interactive.
  • It’s also used server-side (Node.js) and in mobile apps.
  • Write JavaScript in <script> tags in HTML or in .js files.
<script>
  console.log('Hello, JavaScript!');
</script>

Variables and Data Types

  • Declare variables with var, let, or const.
  • let allows reassignment; const doesn’t.
  • Data types: number, string, boolean, null, undefined, object, array
let name = 'Alice'; // string
const age = 25; // number
let isStudent = true; // boolean
let scores = [90, 85, 88]; // array
let person = { name: 'Bob', age: 30 }; // object

Operators and Expressions

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, ===, !=, >, <
  • Logical: &&, ||, !
  • Assignment: =, +=, -=
let x = 10;
let y = 5;
console.log(x + y); // 15
console.log(x === 10 && y < 6); // true

Control Flow

  • Conditionals: if, else if, else
  • Loops: for, while, do...while
  • switch for multiple conditions
let score = 85;
if (score >= 90) {
  console.log('A');
} else if (score >= 80) {
  console.log('B');
} else {
  console.log('C');
}

for (let i = 0; i < 3; i++) {
  console.log(i); // 0, 1, 2
}

Functions

  • Define with function keyword or arrow syntax (=>).
  • Parameters and return values make functions reusable.
  • Functions can be stored in variables.
function greet(name) {
  return `Hello, ${name}!`;
}

const add = (a, b) => a + b;

console.log(greet('Alice')); // Hello, Alice!
console.log(add(3, 4)); // 7

Arrays and Objects

  • Arrays store ordered lists; access with indices ([0]).
  • Objects store key-value pairs; access with dot (.) or bracket ([]) notation.
  • Common methods: push, pop, map, filter(arrays); Object.keys() (objects).
let fruits = ['apple', 'banana', 'orange'];
fruits.push('grape');
console.log(fruits[1]); // banana

let user = { name: 'Charlie', age: 28 };
console.log(user.name); // Charlie

DOM Manipulation

  • The Document Object Model (DOM) represents HTML.
  • Use document to select and modify elements.
  • Common methods: querySelector, innerHTML, style
// HTML: <p id="demo">Hello</p>
let para = document.querySelector('#demo');
para.innerHTML = 'World!';
para.style.color = 'blue';

Event Handling

  • Events (click, input, etc.) trigger JavaScript code.
  • Use addEventListener to attach handlers.
// HTML: <button id="btn">Click Me</button>
let button = document.querySelector('#btn');
button.addEventListener('click', () => {
  alert('Button clicked!');
});

Error Handling

  • Use try-catch to handle errors gracefully
  • Throw custom errors with throw
try {
  let result = riskyOperation();
} catch (error) {
  console.log('Error:', error.message);
}

ES6+ Features

  • let and const for block scope.
  • Template literals: Hello, ${name}!.
  • Destructuring: extract values from arrays/objects.
const [a, b] = [1, 2]; // a = 1, b = 2
const { name, age } = { name: 'Dave', age: 40 };
console.log(`Name: ${name}, Age: ${age}`); // Name: Dave, Age: 40

Asynchronous JavaScript

  • JavaScript is single-threaded; async operations (e.g., fetching data) use callbacks, Promises, or async/await
  • Promises resolve or reject; chain with .then()
  • async/await simplifies Promise handling.
fetch('https://api.example.com/data')
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.log('Error:', error));

async function getData() {
  try {
    let response = await fetch('https://api.example.com/data');
    let data = await response.json();
    console.log(data);
  } catch (error) {
    console.log('Error:', error);
  }
}

Class and Object-Oriented Programming (OOP)

  • Classes are blueprints for creating objects.
  • Use class keyword to define a class.
  • Constructor initializes object properties.
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  // instance method
  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
  // static class method
  static species() {
    return 'Homo sapiens';
  }
}
// Create an instance of the class note the use of `new`
const alice = new Person('Alice', 25);
alice.greet(); // Hello, my name is Alice
const bob = new Person('Bob', 30);
bob.greet(); // Hello, my name is Bob

// Using a static method is called on the class itself not on instances
console.log(Person.species()); // Homo sapiens

Mini Project Dice Game

Build a simple dice game to apply what you’ve learned. The code is included in this repository.

HTML (index.html)

<!DOCTYPE html>
<html>
  <head>
    <title>JS Dice Game</title>
  </head>
  <body>
    <h1>JS Dice Game</h1>
    <script src="dice.js"></script>
  </body>
</html>

JavaScript (dice.js)

const rollDice = () => Math.floor(Math.random() * 6) + 1;

const playGame = () => {
  let player1Score = 0;
  let player2Score = 0;

  for (let i = 0; i < 5; i++) {
    const player1Roll = rollDice();
    const player2Roll = rollDice();
    console.log(
      `Round ${
        i + 1
      }: Player 1 rolled ${player1Roll}, Player 2 rolled ${player2Roll}`
    );

    if (player1Roll > player2Roll) {
      player1Score++;
      console.log('Player 1 wins this round!');
    } else if (player2Roll > player1Roll) {
      player2Score++;
      console.log('Player 2 wins this round!');
    } else {
      console.log("It's a tie!");
    }
  }

  console.log(
    `Final Score: Player 1 - ${player1Score}, Player 2 - ${player2Score}`
  );
  if (player1Score > player2Score) {
    console.log('Player 1 wins the game!');
  } else if (player2Score > player1Score) {
    console.log('Player 2 wins the game!');
  } else {
    console.log('The game is a tie!');
  }
};

playGame();

How it works: The game simulates rolling two players rolling a dice for five rounds, keeping track of the scores and announcing the winner.

Conclusion

Congratulations! You’ve learned the essentials of JavaScript in just 60 minutes. This crash course covered variables, data types, control flow, functions, arrays, objects, DOM manipulation, event handling, error handling, ES6+ features, and asynchronous JavaScript. You also built a simple dice game to apply your knowledge.

Additional Resources

What’s Next?

  • Explore more advanced topics like closures, prototypes, and ES6 modules.
  • Build more projects to solidify your understanding.
  • Join JavaScript communities (e.g., Stack Overflow, Reddit) for support and resources.
  • Follow JavaScript blogs and YouTube channels for updates and tutorials.
  • Read books like "Eloquent JavaScript" or "You Don’t Know JS" for deeper insights.
  • Experiment with JavaScript libraries and frameworks (e.g., React, Vue.js).
  • Practice on sites like freeCodeCamp or LeetCode.
  • Explore frameworks like React or Vue.js.
  • Learn Node.js for backend development.
  • Refer to MDN Web Docs for in-depth JavaScript documentation.

Contributing

Found a typo or want to improve the code? Feel free to open an issue or submit a pull request!

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

  • Inspired by the need for fast, practical JavaScript learning.
  • Thanks to the JavaScript community and resources like MDN Web Docs.

Happy coding!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published