LearnFrenzy
Sign in Take a free test
JS

JavaScript Programming · Topic 1 of 25

JavaScript Basics Questions and Concepts

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Introduction & Syntax

JavaScript Basics · Study material

JavaScript is the language of the web: every browser runs it, and with Node.js it also runs servers, command-line tools and APIs. It is usually the first language a front-end, full-stack or MERN developer is tested on – and its basics hide rules that interviewers love: automatic semicolon insertion, why typeof null is "object", what strict mode changes, and why a single syntax error stops a whole script. This topic covers how JavaScript runs and its core syntax. Then test yourself with 60 practice questions – 15 MCQs, 20 predict-the-output puzzles, 15 debugging questions and 10 coding problems – every one run on Node.js. Each example below has a Run button: try it in your browser, edit it and run it again.

What JavaScript is

TermMeaning
ECMAScriptthe language specification, updated every year by the TC39 committee (ES6 = ES2015 was the big update)
JavaScript enginethe program that runs JavaScript: V8 (Chrome, Edge, Node.js), SpiderMonkey (Firefox), JavaScriptCore (Safari)
Runtimeengine + extra APIs: the browser adds document, window, fetch; Node.js adds fs, process, http
JIT compilationmodern engines parse the code, then compile hot parts to machine code while it runs
Single-threadedone call stack runs your code; the event loop handles timers, network and events
Dynamically typedvariables have no declared type; values do

A program needs no main() or class: statements run from the top of the file to the bottom.

// A JavaScript program is a list of statements, run from top to bottom
console.log("Hello, LearnFrenzy!");          // prints one line
console.log("JavaScript", 2026, true);       // several values: separated by a space
const language = "JavaScript";
console.log(`I am learning ${language}`);    // template literal: ${ } inserts a value

Output

Hello, LearnFrenzy!
JavaScript 2026 true
I am learning JavaScript

Printing with console.log()

console.log(...values) prints its arguments separated by spaces and ends the line. Strings print without quotes on their own, but inside arrays and objects Node.js and the browser console show them in quotes. console.log itself returns undefined.

// console.log prints any value – Node and browser consoles show arrays and objects like this
console.log(42, "42");                 // a number and a string look alike when printed on their own…
console.log([1, "two", true]);         // …but inside an array the string keeps its quotes
console.log({ name: "Asha", marks: 91 });
console.log(console.log === console.log, typeof console.log);

Output

42 42
[ 1, 'two', true ]
{ name: 'Asha', marks: 91 }
true function

Statements, semicolons and ASI

Statements end with a semicolon. If you leave one out, the parser applies Automatic Semicolon Insertion: at a line break where the next token cannot continue the statement, it inserts one. Two rules matter in interviews:

  • After return, break, continue and throw, a line break always ends the statement – so return on its own line returns undefined.
  • A line that starts with (, [ or ` is joined to the previous line – let b = a followed by [1, 2].forEach(…) becomes a[1, 2].forEach(…).
// Automatic Semicolon Insertion (ASI): a line break right after return ends the statement
function broken() {
  return          // ← ASI inserts a semicolon here
    "result";     // never reached
}
function fixed() {
  return "result";   // keep the value on the same line as return
}
console.log(broken());
console.log(fixed());

Output

undefined
result

Comments, identifiers and reserved words

// … comments to the end of the line; /* … */ spans several lines but cannot be nested – the first */ ends it. Identifiers may contain letters, digits, _ and $, must not start with a digit and are case-sensitive. Reserved words such as class, const, function, return, typeof and new cannot be names; undefined, NaN and Infinity are not reserved but are read-only globals. By convention variables and functions use camelCase, classes PascalCase and constants UPPER_SNAKE_CASE.

// Identifiers: letters, digits, _ and $ – not starting with a digit. Case matters.
const $price = 499;
const _discount = 50;
const finalPrice2 = $price - _discount;
const total = 1, Total = 2, TOTAL = 3;     // three different variables
console.log(finalPrice2);
console.log(total + Total + TOTAL);

Output

449
6

Template literals

Strings may use single quotes, double quotes or backticks. Only backticks (template literals) evaluate ${expression} and keep line breaks.

const name = "Rahul";
const marks = 87;
// Backticks: ${expression} is evaluated, and the string may span several lines
const report = `Student: ${name}
Marks:   ${marks}/100
Result:  ${marks >= 40 ? "Pass" : "Fail"}`;
console.log(report);
console.log('Single quotes do not interpolate: ${marks}');

Output

Student: Rahul
Marks:   87/100
Result:  Pass
Single quotes do not interpolate: ${marks}

typeof at a glance

JavaScript has seven primitive types – string, number, bigint, boolean, undefined, symbol and null – and objects (arrays and functions are objects). typeof returns a string naming the type, with two famous exceptions: typeof null is "object" and typeof a function is "function". The Data Types topic covers them in depth.

// typeof returns the type of a value as a string
console.log(typeof 3.14, typeof "hi", typeof true);
console.log(typeof undefined, typeof 10n, typeof Symbol("id"));
console.log(typeof null);           // "object" – a bug kept since 1995 for compatibility
console.log(typeof function () {}, typeof [], typeof {});

Output

number string boolean
undefined bigint symbol
object
function object object

Strict mode

"use strict"; at the top of a script or function turns silent mistakes into errors: assigning to an undeclared variable throws a ReferenceError, this is undefined in a plain function call, duplicate parameter names and the with statement are syntax errors. ES modules and class bodies are always strict, so modern code is strict by default.

// Modules (and this example) are always in strict mode: mistakes throw instead of failing silently
try {
  undeclared = 10;                  // no let/const/var – a ReferenceError in strict mode
} catch (e) {
  console.log("Caught:", e.name);
}
function whoAmI() {
  return this;                      // a plain call in strict mode: this is undefined
}
console.log(whoAmI());

Output

Caught: ReferenceError
undefined

A script is parsed before it runs

The engine parses the whole script first. A SyntaxError anywhere – a missing bracket, a reserved word used as a name, a let declared twice in the same scope – stops the script before line 1 runs. Errors such as ReferenceError and TypeError happen at run time, only when the faulty line is reached.

Common mistakes

  • Value on the line after return – the function returns undefined.
  • Leaving out semicolons before ( or [ – the lines join and throw a TypeError.
  • Wrong case – console.Log is not a function; Price and price are different names.
  • ${ } inside quotes – only backticks interpolate.
  • "5" + 3 – + with a string joins text: "53", not 8.
  • Using document in Node.js – the DOM belongs to the browser, not to the language.

Key points to remember

  • JavaScript implements ECMAScript; V8 runs it in Chrome and Node.js.
  • console.log prints its arguments separated by spaces and returns undefined.
  • A line break after return always ends the statement.
  • Identifiers are case-sensitive and may use $ and _.
  • Only template literals (backticks) interpolate ${ }.
  • typeof null is "object"; typeof a function is "function".
  • Modules and classes are always strict; "use strict" makes scripts strict.
  • Syntax errors stop the whole script before it starts.

Frequently asked questions

Is JavaScript compiled or interpreted?

Both. Engines parse the source, start running it with an interpreter and compile frequently used code to machine code just in time (JIT). There is no compile step you run yourself.

Is JavaScript the same as Java?

No. The names are a 1995 marketing decision. Java is a statically typed, class-based language compiled to bytecode; JavaScript is dynamically typed, prototype-based and runs in browsers and Node.js.

Should I write semicolons?

Most style guides do, and it avoids the ASI traps above. If a team leaves them out, a line must never start with ( [ or ` without a leading semicolon.

Where can I learn more?

Read What is JavaScript?, let, const and var and JavaScript Data Types in the JavaScript tutorial, then practise with the exercises below.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About JavaScript Programming

Why it matters

  • JavaScript runs every web page and, with Node.js, servers, tools and APIs – it is the most used language in the world
  • Front-end, full-stack and MERN roles test JavaScript first, and many campus coding rounds allow it
  • "Predict the output" questions on hoisting, closures, this, coercion and the event loop are interview favourites
  • Modern ES6+ – let/const, arrow functions, destructuring, promises and async/await – is what companies expect from freshers

Who it is for

  • College students (B.Tech, BCA, MCA, B.Sc CS) learning web development and preparing for semester exams and viva
  • Freshers preparing for campus placements, online assessments and front-end interviews
  • Developers moving to React, Angular, Vue or Node.js who need strong core JavaScript
  • Anyone revising ES6 and asynchronous JavaScript before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs on concepts and ES6 features
  • "Predict the output" puzzles – hoisting, coercion, closures, promises and timers
  • "Find the error" debugging: SyntaxError, ReferenceError, TypeError and silent logic bugs
  • Coding problems with solutions and real output, every one run on Node.js – run any study example in your browser

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture, Cognizant tests
  • Front-end developer interviews
  • MERN / full-stack interviews
  • Online coding assessments
  • University practicals

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, debugging, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links