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
| Environment | Example |
|---|---|
| Browser | Chrome, Firefox, Safari |
| Server | Node.js |
| Mobile | React Native |
| Desktop | Electron |
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
| Feature | JavaScript | Java |
|---|---|---|
| Type | Dynamic | Static |
| Compiled? | Interpreted/JIT | Compiled |
| OOP | Prototype-based | Class-based |
| Runs in | Browser / Node | JVM |
ECMAScript Versions (Important)
| Version | Year | Key Feature |
|---|---|---|
| ES5 | 2009 | strict mode, JSON |
| ES6/ES2015 | 2015 | let/const, arrow functions, classes, promises |
| ES2017 | 2017 | async/await |
| ES2020 | 2020 | Optional chaining ?., Nullish coalescing ?? |
| ES2022 | 2022 | Top-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.