D Dev Notebook

JavaScript Intro

JavaScript is a versatile scripting language primarily known for its role in creating interactive and dynamic web pages

dev notebook

JavaScript is a high-level, versatile programming language primarily used for adding interactivity and dynamic behavior to websites. It runs in the browser, meaning it can manipulate web page elements, respond to user actions, validate input, and more without needing to reload the page. JavaScript is one of the core technologies of web development, alongside HTML (structure) and CSS (style).

History

In 1995, JavaScript was created by a Netscape developer named Brendan Eich. First, its name was Mocha. And then, its name was changed to LiveScript.

  • 1995 - JavaScript was invented by Brendan Eich
  • 1997 - ECMAScript 1 was released (European Computer Manufacturers Association)
  • 1998 - ECMAScript 2 was released
  • 1999 - ECMAScript 3 was released
  • 2008 - ECMAScript 4 was abandoned
  • 2009 - ECMAScript 5 was released
  • 2015 - ECMAScript 6 was released
  • 2018 - Full support for ES6 in all browsers (ES6)

Why JavaScript?

  • JavaScript is easy to learn.
  • JavaScript is the world's most popular programming language. As of 2022, 98% of websites use JavaScript.
  • JavaScript can update and change both HTML and CSS.
  • JavaScript can calculate, manipulate and validate data.

Features

  • Dynamic content: Can modify web content (e.g., changing text or images).
  • Interactivity: Supports event-driven programming, allowing things like button clicks and form submissions to trigger code execution.
  • Widely supported: Works across all modern web browsers.
  • Client-side: Runs in the browser on the user's machine.
  • Server-side: Runs on a web server to process requests before sending responses to the client.

How to use js in the web pages?

  • Inline JavaScript: Adding JavaScript code directly into an HTML element’s attribute.
<button onclick="console.log(‘Hello, World’)">Click Me</button> 
  • Internal JavaScript: Writing JavaScript code inside a <script> tag within the HTML file.
<script> document.write(‘Hello, world’) </script> 
  • External JavaScript: Writing JavaScript code in a separate file (with .js extension) and linking to it from the HTML using the <script> tag.
<script src="script.js"></script>

Syntax and Statement

Syntax:

JavaScript syntax is the set of rules that defines how JavaScript programs are build. Understanding the syntax is the foundation for writing JavaScript code.

Statement:

A JavaScript program is a series of statements, and each statement performs a specific task. Each statement typically ends with a semicolon (;), though it's not always mandatory.

Case Sensitivity:

JavaScript is case-sensitive. For example, myVariable and myvariable are treated as two different variables.

Comments

Comments are lines in code that are ignored by the JavaScript engine. They are useful for explaining parts of the code, making it easier to understand and maintain.

Single-line comments:

// This is a single-line comment 

Multi-line comments:

/* This is a multi-line comment */

On this page