Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Chat Application in JavaScript

Render a local chat in JavaScript with form events, textContent, and a delayed demo reply. Messages stay in this tab; no network or second person is involved.

AdvancedJavaScript Mini ProjectsExample 17 of 20

Save this as index.html and open it in a browser.

chat-application.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<h1>Local chat demo</h1><p>No network: messages stay in this tab.</p><ol id="messages" aria-live="polite"></ol><form id="chat"><label>Message <input id="message" maxlength="200" required></label><button>Send</button></form>
7<script>
8const messages = [];
9const list = document.querySelector('#messages');
10function appendMessage(sender, text) {
11 messages.push({ sender, text });
12 const row = document.createElement('li'); row.textContent = sender + ': ' + text; list.append(row);
13}
14document.querySelector('#chat').onsubmit = event => {
15 event.preventDefault();
16 const input = document.querySelector('#message');
17 const text = input.value.trim(); if (!text) return;
18 appendMessage('You', text); input.value = '';
19 setTimeout(() => appendMessage('Demo helper', 'Received locally: ' + text), 300);
20};
21</script>
22</body>
23</html>

Sample Output

You: When does the lab open?

The form handler appends this locally, immediately.

Demo helper: Received locally: When does the lab open?

A local timeout callback appends a simulated reply. No remote user received anything.

In this one

  • Render locally submitted messages
  • Capture each reply text correctly
  • Show a delayed simulated response
  • Keep message content out of HTML execution

How it works

  1. 1

    Keep an array and visible list for local messages.

  2. 2

    Prevent the form from reloading the page.

  3. 3

    Trim the input and append a You message.

  4. 4

    Schedule a fixed local echo using the captured text.

  5. 5

    Read the immediate message and later demo reply; no network delivery takes place.

Edge cases

Whitespace message

A required field can still contain only spaces.

Trim before appending so the conversation does not fill with empty-looking rows.

Several quick sends

Delayed callbacks must remember their own message.

The handler captures a separate const text for each submission, so replies do not all use the latest input.

What's going on

This chat application is a local page demo that appends messages and produces a fixed delayed reply. It lets a student practise form handling and message rendering without pretending to connect two people over the internet.

Submitting a non-empty message immediately adds a You row. A short timeout then adds a Demo helper row that repeats the captured text. The timeout is only a simulation of waiting, not a network connection or a person reading the message. The messages array holds the conversation in this tab until refresh.

Each row uses textContent so typed tags remain text rather than running as page code. The list uses aria-live polite to make new content available to assistive tools without forcing an urgent interruption. A real multi-user chat needs a server, sign-in, delivery rules, and careful handling of abuse and private data. None of those are hidden inside this example.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Who writes the Demo helper response?

Pick an answer to check it.

Question 2

Why capture text in const?

Pick an answer to check it.

Questions people ask

Does Send contact another student?

No. This demo has no server or network calls. The second message is created in the same tab.

Why does the reply wait?

The short delay illustrates later work. It does not indicate actual delivery or reading.

Why not use innerHTML for messages?

A message may contain tags or scripts. textContent displays the characters without executing them.