Introduction to JavaScript

Jun 5, 2024

Introduction to JavaScript

Frequently Asked Questions

What is JavaScript?

  • One of the most popular programming languages.
  • Used by big companies like Netflix, Walmart, and PayPal.
  • Average salary of a JavaScript developer in the US: $72,000/year.
  • Roles: Front-end developer, Back-end developer, Full Stack developer.

What can you do with JavaScript?

  • Initially used to build interactive web pages.
  • Now can be used to build full-blown web/mobile apps, real-time networking applications, command-line tools, and games.
  • Huge community support and investments by large companies.

Where does JavaScript code run?

  • Originally designed to run only in browsers.
  • Each browser has a JavaScript engine (e.g., SpiderMonkey for Firefox, V8 for Chrome).
  • Node.js allows JavaScript to run outside of browsers (C++ program including Google’s V8).
  • JavaScript can be run inside browsers and in Node.js runtime environment.

Difference between JavaScript and ECMAScript?

  • ECMAScript (ES) is a specification JavaScript conforms to.
  • Maintained by ECMA organization.
  • ECMAScript 2015 (ES6) introduced many new features.

Practical JavaScript

  1. Browser JavaScript Console

    • Demonstrates how to use the browser's console to execute JavaScript code.
    • Example: console.log('Hello World');
  2. Setting Up Development Environment

    • Tools: Visual Studio Code (VS Code) and Node.js.
    • Instructions on downloading and installing these tools.
  3. Basic HTML Integration

    • Create a folder and a new file, index.html.
    • Use boilerplate HTML to host JavaScript code.
    • Install the Live Server extension.
    • Embed JavaScript code using a <script> element.
    • Best practice: Place <script> at the end of the body tag.
  4. JavaScript and Node.js

    • Instructions on running JavaScript code in Node.js.
    • VS Code integration for an easier workflow.

JavaScript Fundamentals

Variables

  • Use let and const to declare variables. Avoid var.
  • Example: let name;, const interestRate = 0.3;
  • Rules for naming variables:
    • Cannot be a reserved keyword.
    • Should be meaningful.
    • Cannot start with a number.
    • No spaces or hyphens; use camelCase.
    • Are case-sensitive.

Constants

  • Declare constants using const.
  • Cannot be reassigned.

Primitive Types

  • String: let name = 'Mosh';
  • Number: let age = 30;
  • Boolean: let isApproved = true;
  • Undefined: let firstName; (or explicitly let firstName = undefined;)
  • Null: let selectedColor = null;

Dynamic Typing

  • JavaScript is a dynamic language; variable types can change at runtime.

Objects

  • Group related variables together.
  • Created using object literals: let person = { name: 'Mosh', age: 30 };
  • Accessing properties: Dot notation (person.name) or Bracket notation (person['name']).

Arrays

  • Store lists: let selectedColors = ['red', 'blue'];
  • Arrays are dynamic in length and type.
  • Accessing elements: selectedColors[0];
  • Arrays inherit properties and methods such as .length.

Functions

  • A set of statements performing a task or calculating a value.
  • Function declaration: function greet(name) { console.log('Hello ' + name); }
  • Function call: greet('John');
  • Functions with multiple parameters: function greet(firstName, lastName) { ... }
  • Returning values: function square(number) { return number * number; }

Best Practices and Tools

  • For complex applications, separate JavaScript from HTML for better maintainability.
  • Utilize integrated terminal in VS Code for running Node.js scripts directly within the editor.

  • End with a recommendation to enroll in a comprehensive JavaScript course for further learning.