Introduction to JavaScript Basics

Aug 22, 2024

JavaScript Introduction Notes

Frequently Asked Questions

  1. What is JavaScript?

    • One of the most popular programming languages.
    • Used by companies like Netflix, Walmart, and PayPal.
    • Average salary in the U.S.: $72,000/year.
    • Roles: Front-end, Back-end, Full Stack Developer.
  2. What can you do with JavaScript?

    • Initially used for interactive web pages.
    • Can build web/mobile apps, real-time networking apps (chats, video streaming), command-line tools, and games.
  3. Where does JavaScript code run?

    • Originally in browsers (JavaScript engines like SpiderMonkey in Firefox and V8 in Chrome).
    • Node.js allows execution outside of browsers, enabling backend development.
  4. What is the difference between JavaScript and ECMAScript?

    • ECMAScript is a specification; JavaScript is a programming language based on this specification.
    • ECMAScript versions have been updated annually, with ES6 (2015) introducing many new features.

Writing JavaScript Code

  • JavaScript can be written in the console of any browser (e.g., Chrome).
  • Basic syntax example:
    console.log('Hello World');  
    
  • Use semicolons to terminate statements.

Setting Up Development Environment

  1. Code Editor:
    • Recommended: Visual Studio Code (download from code.visualstudio.com).
    • Alternatives: Sublime Text, Atom.
  2. Node.js:
    • Download from nodejs.org.
    • Use Node.js for installing third-party libraries.

Creating a Project

  1. Create a folder named js-basics.
  2. Drag and drop it into Visual Studio Code.
  3. Create a new file index.html and add basic HTML boilerplate.
  4. Install Live Server extension in VS Code for serving applications.
  5. Create a new file index.js for JavaScript code.
  6. Reference index.js in index.html:
    <script src="index.js"></script>  
    

JavaScript Basics

Variables

  • Used to store data temporarily.
  • Declaration: let name; (modern best practice)
    • Use const for constants (values that don't change).
  • Variable Naming Rules:
    1. Cannot use reserved keywords.
    2. Must be meaningful.
    3. Cannot start with a number.
    4. Cannot contain spaces or hyphens (use camelCase).

Primitive Types

  1. String:
    • Example: let name = 'Mosh';
  2. Number:
    • Example: let age = 30;
  3. Boolean:
    • Example: let isApproved = true;
  4. Undefined:
    • Default value of uninitialized variables.
  5. Null:
    • Explicitly empty value.
  6. Symbol:
    • New in ES6 (not covered in detail).

Dynamic Typing

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

Objects

  • Used to group related data:
    let person = {  
        name: 'Mosh',  
        age: 30  
    };  
    
  • Accessing properties:
    • Dot notation: person.name
    • Bracket notation: person['name']

Arrays

  • Used to store lists of items:
    let colors = ['red', 'blue'];  
    
  • Accessing elements using indices: colors[0].
  • Arrays can hold different types of data.

Functions

  • A block of code that performs a task:
    function greet(name) {  
        console.log('Hello ' + name);  
    }  
    greet('Mosh');  
    
  • Can take parameters and return values.

Conclusion

  • This introduction covers the basics and prepares for more advanced topics in JavaScript.
  • Encouragement to enroll in the complete JavaScript course for deeper learning.