Skip to main content

Introduction to JavaScript

JavaScript (JS) is a lightweight, interpreted, object-oriented programming language with first-class functions. It is best known as the scripting language for Web pages, but also runs in non-browser environments such as Node.js.

  • Created by Brendan Eich in 1995 at Netscape.
  • Originally called Mocha, then LiveScript, finally JavaScript.
  • Standardized as ECMAScript (ES) by ECMA International.
  • Latest widely-supported version: ES2022+.

Where JavaScript Runs

EnvironmentExample
BrowserChrome, Firefox, Safari
ServerNode.js
MobileReact Native
DesktopElectron

How to Run JavaScript

1. Browser Console

// Right click → Inspect → Console → type JS code

2. <script> tag in HTML

<!DOCTYPE html>
<html>
<head><title>JS Demo</title></head>
<body>
<script>
console.log("Hello, World!");
</script>
</body>
</html>

3. External .js file

<script src="app.js"></script>

4. Node.js terminal

node app.js

JavaScript vs Java

FeatureJavaScriptJava
TypeDynamicStatic
Compiled?Interpreted/JITCompiled
OOPPrototype-basedClass-based
Runs inBrowser / NodeJVM

ECMAScript Versions (Important)

VersionYearKey Feature
ES52009strict mode, JSON
ES6/ES20152015let/const, arrow functions, classes, promises
ES20172017async/await
ES20202020Optional chaining ?., Nullish coalescing ??
ES20222022Top-level await, at() method

console Methods

console.log("Normal output");
console.error("Error message");
console.warn("Warning message");
console.table([{a: 1}, {a: 2}]);
console.time("timer");
console.timeEnd("timer");
console.group("Group");
console.groupEnd();

Strict Mode

"use strict";
// Prevents use of undeclared variables
x = 10; // ReferenceError in strict mode
  • Eliminates silent errors.
  • Prevents use of reserved future keywords.
  • Makes debugging easier.

MCQ — Introduction to JavaScript

Q1: Who created JavaScript?

A) James Gosling B) Brendan Eich ✅ C) Linus Torvalds D) Tim Berners-Lee

Answer: B — Brendan Eich created JavaScript in 1995.

Q2: What does ECMAScript refer to?

A) A JavaScript framework B) The standard specification that JavaScript is based on ✅ C) A CSS preprocessor D) A Node.js package manager

Answer: B

Q3: Which tag is used to include JavaScript in HTML?

A) <js> B) <javascript> C) <script>D) <code>

Answer: C

Q4: What happens in strict mode when you use an undeclared variable?

A) It creates a global variable B) It returns undefined C) It throws a ReferenceError ✅ D) It logs a warning

Answer: C

Q5: In which ES version were Promises introduced?

A) ES5 B) ES6 (ES2015) ✅ C) ES2017 D) ES2020

Answer: B

Q6: Which of the following is NOT a valid way to run JavaScript?

A) Browser console B) Node.js C) <script> tag D) <style> tag ✅

Answer: D

Q7: async/await was introduced in which ES version?

A) ES6 B) ES2016 C) ES2017 ✅ D) ES2018

Answer: C

Q8: JavaScript is ___ typed.

A) Statically B) Strongly C) Dynamically ✅ D) Weakly and statically

Answer: C — Types are determined at runtime, not compile time.