💻

Getting Started with JavaScript

May 8, 2025

An Introduction to JavaScript

What is JavaScript?

  • JavaScript is a core web development technology alongside HTML and CSS.
    • HTML structures web content.
    • CSS manages styling and layout.
    • JavaScript adds interactivity, logic, and advanced features.
  • Created by Brendan Eich in 1995 for Netscape Navigator.
  • Enhances web pages with interactivity and dynamic content.
  • Typical use cases:
    • Form validation
    • Animations and transitions
    • Interactive content
    • Fetching and displaying data
  • Can be used for client-side and server-side scripting.

Using This Guide

  • Provides a comprehensive intro to JavaScript concepts.
  • Focuses on quick references for operators, functions, and syntax.

Experimenting with JavaScript

  • Use the <script> tag to include JavaScript in HTML.
  • Best practice: Place JavaScript at the bottom of HTML pages.
  • Example code: <!DOCTYPE html> <html> <head> <title>My First JavaScript</title> </head> <body> <h2>My First JavaScript Program</h2> <button onclick="displayMessage()">Click me</button> <p id="demo"></p> <script> function displayMessage() { document.querySelector('#demo').textContent = 'Welcome to your first JavaScript program.'; } </script> </body> </html>

Variables, Data Types, and Operators

  • Variables: var, let, const
    • let and const preferred over var for block scoping and immutability.
  • Data Types: Number, String, Boolean, Symbol, Object
    • Numbers: Integers and floating-point numbers
    • Strings: Text enclosed in quotes
    • Booleans: true or false
    • Symbols: Unique identifiers
    • Objects: Containers for values and functions
  • Operators:
    • Arithmetic: +, -, *, /, %
    • Logical: &&, ||, !
    • Comparison: ==, ===, !=, !==, <, >, <=, >=
    • Compound: +=, -=, *=, /=

Control Flow

  • Conditional Statements:
    • if/else: Execute code blocks based on conditions.
    • Nested if: Check multiple conditions.
    • Ternary operator: Shorthand for if/else.
  • Loops:
    • while: Repeatedly execute a block as long as a condition is true.
    • for: Execute a block a specific number of times.
    • for...of: Iterate over iterable objects like arrays.

Object-Oriented Programming (OOP)

  • Organizes code using objects with properties and methods.
  • Classes: Blueprints for creating objects.
    • Use class keyword and constructor method.
  • Inheritance: Allows a class to inherit properties/methods from another.
  • Methods: Functions associated with an object.

Functions

  • Reusable code performing specific tasks.
  • Types:
    • Function Declarations: function myFunction() {}
    • Function Expressions: const myFunc = function() {}
    • Arrow Functions: const myFunc = () => {}

Scope

  • Global Scope: Variables accessible anywhere.
  • Function Scope: Variables accessible within a function.
  • Block Scope: Variables accessible within a block.
  • Lexical Scope: Variables accessible in declaring and nested scopes.

Data Structures

  • Arrays: Store multiple values in a single variable.
    • Methods: push, pop, shift, slice
  • Maps and Sets:
    • Maps: Key-value pairs with any data type as key.
    • Sets: Collections of unique values.

JavaScript Libraries and Frameworks

  • Benefits:
    • Reduced development time
    • Improved code maintainability
    • Access to community resources
  • Popular Examples:
    • jQuery, React, Angular, Node.js

Next Steps

  • Continually learn and practice JavaScript.
  • Explore additional resources and tutorials.
  • Practice by creating and showcasing web projects.