JavaScript Basics

JavaScript Introduction

JavaScript is a powerful, versatile programming language that brings websites to life with interactivity, dynamic content, and modern web applications.

What is JavaScript?

JavaScript is the programming language of the web. HTML gives a page its structure and CSS its look; JavaScript makes it respond: it reacts to clicks and typing, changes content without reloading, and talks to servers. It runs in every browser, and with Node.js it also runs on servers.

<button id="btn">Click me</button>
<script>
  const btn = document.getElementById("btn");
  btn.addEventListener("click", () => {
    btn.textContent = "Clicked!";
  });
</script>

Interactive JavaScript Demo

Code Editor

<h2 id="greeting">Hello!</h2>
<input id="name" placeholder="Type your name">
<button id="btn">Greet me</button>

Preview

How JavaScript Works

JavaScript Fundamentals

// JavaScript is a high-level, interpreted programming language

// It was created in 1995 by Brendan Eich at Netscape
console.log("Hello, JavaScript!");

// JavaScript is:
// - Dynamic and flexible
// - Interpreted (no compilation needed)
// - Object-oriented and functional
// - Event-driven and asynchronous

// Basic variable declaration
let message = "JavaScript makes websites interactive!";

// Function definition
function greetUser(name) {
  return `Hello, ${name}!`;
}

JavaScript was initially created to add interactivity to web pages, but has evolved into a full-featured programming language.

Key Characteristics

  • • Interpreted: No compilation step required
  • • Dynamic typing: Variables can change types
  • • First-class functions: Functions are values
  • • Prototype-based OOP: Flexible object system
  • • Event-driven: Responds to user interactions
  • • Asynchronous: Non-blocking operations

Evolution & Standards

1995: Created by Brendan Eich in 10 days

1997: ECMAScript standard established

2009: ES5 brought major improvements

2015: ES6/ES2015 modern JavaScript

2016+: Annual releases with new features

Today: ES2023 with latest capabilities

JavaScript Applications & Ecosystems

Where JavaScript Runs

🌐 Web Browsers

Client-side scripting for interactive web pages and single-page applications (SPAs).

🖥️ Servers

Node.js enables server-side JavaScript for APIs, databases, and backend services.

📱 Mobile Apps

React Native, Ionic, and Cordova for cross-platform mobile development.

💻 Desktop Apps

Electron enables desktop applications like VS Code, Discord, and Slack.

🎮 Games

Browser games, game engines, and interactive entertainment experiences.

🤖 IoT & AI

Machine learning with TensorFlow.js and IoT device programming.

Core JavaScript Concepts

Fundamental Programming Concepts

// Variables and data types
let name = "Alice"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let hobbies = ["reading", "coding"]; // Array
let person = {name, age, isStudent}; // Object

// Functions
function calculateArea(width, height) {
  return width * height;
}

// Control structures
if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

// Loops
for (let hobby of hobbies) {
  console.log(`I enjoy ${hobby}`);
}

JavaScript provides all the fundamental programming constructs you need to build complex applications.

DOM Manipulation

// Find and modify HTML elements
let button = document.getElementById('myButton');

// Add event listener
button.addEventListener('click', function() {
  alert('Button clicked!');
});

// Change content and style
document.querySelector('h1').textContent = 'New Title';
document.body.style.backgroundColor = 'lightblue';

Interact with and modify web page content dynamically

Asynchronous Programming

// Fetch data from an API
async function fetchUserData() {
  try {
    let response = await fetch('/api/user');
    let user = await response.json();
    console.log(user);
  } catch (error) {
    console.error('Error:', error);
  }
}

fetchUserData();

Handle network requests and long-running operations

JavaScript Introduction: summary

Key points

  • JavaScript code runs in the browser as soon as the page loads it; there's nothing to install.
  • It's dynamically typed: a variable can hold any type of value, and types are checked while the code runs.
  • Despite the name, JavaScript is unrelated to Java. The language standard is called ECMAScript.
  • Put <script> tags at the end of <body>, or use defer, so the HTML exists before your code runs.
  • console.log() prints values to the browser console (F12), your main tool for seeing what code does.

Frequently asked questions

›Is JavaScript hard to learn?

It's one of the easiest languages to start with, because you only need a browser and you see results immediately. The tricky parts, such as asynchronous code and this, come later and are covered step by step in this course.

›Should I learn HTML and CSS before JavaScript?

Yes, at least the basics. JavaScript on the web mostly changes HTML and CSS, so you need to know what you're changing.

›What is the difference between Java and JavaScript?

They're completely different languages that share part of a name for 1990s marketing reasons. Java is compiled and statically typed; JavaScript is dynamic and runs natively in browsers.