JavaScript Crash Course – Lesson 1: Introduction to JavaScript

What is JavaScript?

JavaScript (JS) is a versatile, high-level programming language primarily used for creating interactive web pages. It’s an essential part of web development alongside HTML and CSS.

Why Learn JavaScript?

  1. Ubiquity: It’s supported by all modern web browsers.
  2. Versatility: Used for front-end, back-end, mobile, and desktop development.
  3. Large Community: Extensive resources and libraries available.

Setting Up Your Environment

To start coding in JavaScript, you only need:

  1. A text editor (e.g., Visual Studio Code, Sublime Text)
  2. A web browser (e.g., Chrome, Firefox)

Your First JavaScript Program

Let’s write a simple “Hello, World!” program:

JavaScript
console.log("Hello, World!");

To run this:

  1. Open your text editor.
  2. Type the code above.
  3. Save the file as hello.js.
  4. Open your web browser’s console (usually by pressing F12).
  5. Copy and paste the code into the console and press Enter.

You should see “Hello, World!” printed in the console.

Basic Syntax

Variables

Variables are used to store data. In modern JavaScript, we use let and const:

JavaScript
let name = "John";
const age = 30;

Data Types

JavaScript has several basic data types:

  1. String: Text
JavaScript
   let greeting = "Hello";
  1. Number: Integers and floating-point numbers
JavaScript
   let count = 42;
   let price = 19.99;
  1. Boolean: True or false
JavaScript
   let isActive = true;
  1. Undefined: Variable declared but not assigned a value
JavaScript
   let unknownValue;
  1. Null: Intentional absence of any object value
JavaScript
   let emptyValue = null;

Basic Operators

  1. Arithmetic: +, -, *, /, %
  2. Comparison: ==, ===, !=, !==, >, <, >=, <=
  3. Logical: && (AND), || (OR), ! (NOT)

Conditional Statements

Use if, else if, and else for decision making:

JavaScript
let hour = 14;

if (hour < 12) {
    console.log("Good morning!");
} else if (hour < 18) {
    console.log("Good afternoon!");
} else {
    console.log("Good evening!");
}

Practice Exercise

Try creating a variable with your name and use it in a greeting:

JavaScript
let yourName = "Your Name Here";
console.log("Hello, " + yourName + "! Welcome to JavaScript.");

Conclusion

This lesson covered the very basics of JavaScript. In the next lesson, we’ll dive deeper into functions, arrays, and objects.

Remember, practice is key in programming. Experiment with the concepts we’ve covered and don’t hesitate to explore further!