JavaScript Basics

Build strong programming fundamentals for modern web development

Beginner Friendly Hands-on Examples SEO Ready

Table of Contents

Introduction to JavaScript

What You Will Learn:

This tutorial explains what JavaScript is, how it works in browsers, and how to use core concepts like variables, functions, DOM manipulation, events, and asynchronous code.

JavaScript is the programming language of the web. It allows developers to make pages interactive, update content dynamically, validate forms, and connect web apps to APIs and backend services.

Interactivity

Respond to user actions like clicks, typing, and scrolling.

Logic

Write conditions, loops, and reusable functions for app behavior.

Integration

Connect frontend UI with APIs, data, and external services.

JavaScript is a high-level, interpreted programming language used to create dynamic and interactive web pages. Along with HTML and CSS, it forms the core of modern frontend development.

Key Point: HTML gives structure, CSS provides style, and JavaScript adds behavior.

Your first JavaScript

console.log("Hello, World!");
// Press F12 -> Console to see this.

Program vs Script

Program Script
Usually compiled before running Usually interpreted at runtime
Can run as a standalone application Often runs inside another environment (browser/shell)
Generally bigger system-level software Often automation or page interaction logic

JavaScript started mainly as a scripting language in browsers, but today it is used for full applications too.

Client-side Script vs Server-side Script

Client-side Script
  • Runs in the browser
  • Handles UI interactions
  • Can manipulate DOM instantly
  • Examples: form validation, dynamic menus
Server-side Script
  • Runs on the server
  • Accesses database and secure logic
  • Returns data or rendered HTML to client
  • Example runtime: Node.js

What is Client?

A client is the user-side application (usually a web browser) that requests data and displays webpages. It sends requests to servers and receives responses like HTML, CSS, JavaScript, and JSON data.

What is Server?

A server is a machine or service that receives requests from clients, processes business logic, fetches data (if needed), and sends responses back to the client over HTTP/HTTPS.

Client-Server Architecture

This architecture powers most web applications: the client sends requests, the server processes them, and returns responses, often with data from a database.

Client-server architecture diagram showing client browser, server, and database communication
High-level view of client, server, and database communication.

JS History

  • 1995: Created by Brendan Eich at Netscape in about 10 days.
  • 1996: Microsoft released JScript for Internet Explorer.
  • 1997: JavaScript standardized as ECMAScript (ECMA-262).
  • 2009: Node.js introduced JavaScript on server-side.
  • 2015+: ES6 and later versions modernized the language significantly.

JS Versions

Version Year Highlights
ES3 1999 Regular expressions, try/catch improvements
ES5 2009 Strict mode, JSON support, array methods
ES6 (ES2015) 2015 let/const, arrow functions, classes, modules
ES2016+ 2016 onward Async/await, optional chaining, nullish coalescing, more

JavaScript Features

JavaScript is powerful because it supports both beginner-friendly syntax and advanced programming patterns used in modern web apps.

JavaScript features overview including ES6 syntax, async programming, and modern web development capabilities
Visual overview of key JavaScript features used in frontend and full-stack development.

Core Language Features

  • Dynamic typing with flexible data structures
  • Functions as first-class values
  • Prototype-based object model
  • Built-in arrays, objects, and JSON support

Modern ES6+ Features

  • let/const, arrow functions, template literals
  • Destructuring, spread/rest operators
  • Promises and async/await for asynchronous code
  • Modules, optional chaining, and nullish coalescing

10 Interview Questions + 10 MCQs

Interview Pattern 10 Q&A
1Difference between == and ===?easy
Answer: == allows type coercion before comparison; === checks value and type strictly.
2What is lexical scope?medium
Answer: Scope determined by where code is written, enabling inner functions to access outer variables.
3What is a closure?medium
Answer: A function that remembers and uses variables from its lexical environment even after outer execution ends.
4Explain hoisting behavior.medium
Answer: Declarations move to top logically; var initializes as undefined, while let/const stay in TDZ until declaration line.
5How does event bubbling work?easy
Answer: Events start at target and bubble up through ancestors; stop it with event.stopPropagation().
6call vs apply vs bind?medium
Answer: call invokes with comma args, apply invokes with array args, bind returns new bound function.
7What is the event loop?hard
Answer: It coordinates call stack and task queues, executing microtasks before the next macrotask.
8Difference between shallow and deep copy?medium
Answer: Shallow copy duplicates top-level only; nested objects still share reference. Deep copy clones nested data too.
9How do you handle async errors?medium
Answer: Use try...catch with await and fallback UI/logic; also handle promise .catch().
10Common JavaScript memory leak causes?hard
Answer: Unremoved listeners, forgotten timers, large closures, and detached DOM references.

10 JavaScript Interview MCQs

1

Which method creates a new array with elements that pass a condition?

Amap()
Bfilter()
Cfind()
DforEach()
Explanation: filter() returns all matching elements as a new array.
2

What is typeof null?

Anull
Bobject
Cundefined
Dboolean
Explanation: Historical JavaScript behavior returns "object" for null.
3

Which statement about const is true?

ACan always be reassigned
BBlock scoped and must be initialized
CFunction scoped
DNo TDZ
Explanation: const requires initialization and cannot be reassigned.
4

Which storage persists after browser restart?

AsessionStorage
BRuntime memory
ClocalStorage
DTemporary cache only
Explanation: localStorage data persists until explicitly cleared.
5

Which method returns the first matching array element?

Afind()
Bfilter()
Csome()
Devery()
Explanation: find() returns the first matched element, not all matches.
6

Which promise method runs after both success or failure?

Athen()
Bcatch()
Cfinally()
Ddone()
Explanation: finally() executes regardless of outcome.
7

Optional chaining ?. mainly prevents:

ASyntax errors
BType coercion
CAccess errors on null/undefined chain
DInfinite loops
Explanation: It safely short-circuits property access when value is nullish.
8

Which converts JSON string to object?

AJSON.stringify()
BJSON.parse()
CObject.fromJSON()
Dparse.JSON()
Explanation: JSON.parse() parses valid JSON string into JavaScript value.
9

Which keyword marks an async function?

Adefer
Bawait
Casync
Dpromise
Explanation: Functions declared with async return promises.
10

Best strict comparison operator for primitives?

A==
B=
C===
D!=
Explanation: === checks both value and type without coercion.