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].
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).
- Clone this repository:
git clone https://github.com/TheDevPath/Javascript-Crash-Course - Open
index.htmlin a browser to try the to-do list app. - Follow the guide below to learn JavaScript essentials.
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.
- 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.jsfiles.
<script>console.log('Hello, JavaScript!');</script>- Declare variables with
var,let, orconst. letallows reassignment;constdoesn’t.- Data types:
number,string,boolean,null,undefined,object,array
letname='Alice';// stringconstage=25;// numberletisStudent=true;// booleanletscores=[90,85,88];// arrayletperson={name: 'Bob',age: 30};// object- Arithmetic:
+,-,*,/,% - Comparison:
==,===,!=,>,< - Logical:
&&,||,! - Assignment:
=,+=,-=
letx=10;lety=5;console.log(x+y);// 15console.log(x===10&&y<6);// true- Conditionals:
if,else if,else - Loops:
for,while,do...while switchfor multiple conditions
letscore=85;if(score>=90){console.log('A');}elseif(score>=80){console.log('B');}else{console.log('C');}for(leti=0;i<3;i++){console.log(i);// 0, 1, 2}- Define with
functionkeyword or arrow syntax (=>). - Parameters and return values make functions reusable.
- Functions can be stored in variables.
functiongreet(name){return`Hello, ${name}!`;}constadd=(a,b)=>a+b;console.log(greet('Alice'));// Hello, Alice!console.log(add(3,4));// 7- 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).
letfruits=['apple','banana','orange'];fruits.push('grape');console.log(fruits[1]);// bananaletuser={name: 'Charlie',age: 28};console.log(user.name);// Charlie- The Document Object Model (DOM) represents HTML.
- Use
documentto select and modify elements. - Common methods:
querySelector,innerHTML,style
// HTML: <p id="demo">Hello</p>letpara=document.querySelector('#demo');para.innerHTML='World!';para.style.color='blue';- Events (click, input, etc.) trigger JavaScript code.
- Use
addEventListenerto attach handlers.
// HTML: <button id="btn">Click Me</button>letbutton=document.querySelector('#btn');button.addEventListener('click',()=>{alert('Button clicked!');});- Use
try-catchto handle errors gracefully - Throw custom errors with
throw
try{letresult=riskyOperation();}catch(error){console.log('Error:',error.message);}letandconstfor block scope.- Template literals:
Hello, ${name}!. - Destructuring: extract values from arrays/objects.
const[a,b]=[1,2];// a = 1, b = 2const{ name, age }={name: 'Dave',age: 40};console.log(`Name: ${name}, Age: ${age}`);// Name: Dave, Age: 40- JavaScript is single-threaded; async operations (e.g., fetching data) use callbacks, Promises, or
async/await - Promises resolve or reject; chain with
.then() async/awaitsimplifies Promise handling.
fetch('https://api.example.com/data').then((response)=>response.json()).then((data)=>console.log(data)).catch((error)=>console.log('Error:',error));asyncfunctiongetData(){try{letresponse=awaitfetch('https://api.example.com/data');letdata=awaitresponse.json();console.log(data);}catch(error){console.log('Error:',error);}}- Classes are blueprints for creating objects.
- Use
classkeyword to define a class. - Constructor initializes object properties.
classPerson{constructor(name,age){this.name=name;this.age=age;}// instance methodgreet(){console.log(`Hello, my name is ${this.name}`);}// static class methodstaticspecies(){return'Homo sapiens';}}// Create an instance of the class note the use of `new`constalice=newPerson('Alice',25);alice.greet();// Hello, my name is Aliceconstbob=newPerson('Bob',30);bob.greet();// Hello, my name is Bob// Using a static method is called on the class itself not on instancesconsole.log(Person.species());// Homo sapiensBuild 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><scriptsrc="dice.js"></script></body></html>JavaScript (dice.js)
constrollDice=()=>Math.floor(Math.random()*6)+1;constplayGame=()=>{letplayer1Score=0;letplayer2Score=0;for(leti=0;i<5;i++){constplayer1Roll=rollDice();constplayer2Roll=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!');}elseif(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!');}elseif(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.
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.
- 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.
Found a typo or want to improve the code? Feel free to open an issue or submit a pull request!
This project is licensed under the MIT License. See the LICENSE file for details.
- Inspired by the need for fast, practical JavaScript learning.
- Thanks to the JavaScript community and resources like MDN Web Docs.
Happy coding!