JavaScript: Fundamentals and Frequently Asked Questions

Jul 4, 2024

JavaScript: Fundamentals and Frequently Asked Questions

Introduction

  • Questions covered:
    1. What is JavaScript?
    2. What can you do with JavaScript?
    3. Where does JavaScript code run?
    4. What is the difference between JavaScript and ECMAScript?

What is JavaScript?

  • Popular and widely used: Growing faster than any other programming language.
  • Used by big companies: Netflix, Walmart, PayPal.
  • Career prospects: Average salary of a JavaScript developer in the US is $72,000/year.
  • Roles: Front-end developer, back-end developer, full-stack developer.

What can you do with JavaScript?

  • Initial use: Only used in browsers for interactive web pages.
  • Current capabilities:
    • Web and mobile apps
    • Real-time networking applications (chats, video streaming)
    • Command-line tools
    • Games
  • Evolved reputation: No longer seen as a "toy language."

Where does JavaScript code run?

  • Originally: Only in browsers.
  • JavaScript engines:
    • Firefox: SpiderMonkey
    • Chrome: V8
  • Node.js: Introduced by Ryan Dahl in 2009, allows JavaScript to run outside of browsers by embedding V8 inside a C++ program.
  • Runtime environments:
    • Browsers
    • Node.js

Difference between JavaScript and ECMAScript

  • ECMAScript: Specification (standard) for scripting languages.
  • JavaScript: Programming language that conforms to the ECMAScript specification.
  • Standards organization: ECMA International (formerly known as ECMAScript).
  • Versions:
    • First version: 1997
    • Annual releases: Since 2015 (e.g., ECMAScript 2015/ES6)

Using JavaScript in Browsers

  • JavaScript Console: Write and execute JavaScript code directly.
  • Basic syntax demonstration:
    console.log('Hello World');
    alert('Yo');
    
  • Development environment: Recommend using Visual Studio Code and Node.js.
  • Live Server: Extension for serving web applications during development.

Setting Up Development Environment

  • Tools: Visual Studio Code, Node.js.
  • Creating a project: Folder structure, basic HTML template with JavaScript.
  • Live Server setup: Serve web applications and automatic refresh.

Writing JavaScript Code

  • Script placement: Best practice is to place <script> at the end of the <body> to avoid blocking rendering.
  • Variables and Constants:
    • Use let for variables with changing values.
    • Use const for values that shouldn't change.
    • Naming rules: Meaningful names, no reserved keywords, use camelCase.

Primitive Types

  • Types: Strings, Numbers, Booleans, Undefined, Null.
  • Dynamic typing: Variable types can change.

Reference Types

  • Types: Objects, Arrays, Functions.
  • Objects: Collection of key-value pairs.
  • Arrays: Dynamic list of items.
  • Functions:
    • Define reusable blocks of code.
    • Can perform tasks or calculate and return values.
  • Function declaration:
    function greet(name) {
      console.log('Hello ' + name);
    }
    greet('John');
    
  • Return values:
    function square(number) {
      return number * number;
    };
    console.log(square(2)); // Outputs: 4
    

Conclusion

  • Importance of proper setup and understanding JavaScript basics.
  • Encouragement to use these fundamentals to build more complex applications.