Javascript is one of the core building blocks of web development. With its versatility and cross-platform nature, Javascript powers complex front-end applications and frameworks like React, Angular, and Vue. It is also used on the server-side through platforms like Node.js.
This article will provide you with an in-depth introduction to Javascript and test your knowledge through sample multiple choice questions. We will cover key concepts like syntax, data types, operators, functions, as well as recent advancements in the language. By the end, you will have a solid grasp of Javascript fundamentals.
A Brief History of Javascript
Javascript was created in 1995 by Brendan Eich at Netscape as LiveScript. It was renamed to Javascript likely for marketing reasons. The initial goal was adding dynamic interactivity to web pages.
Some key events in Javascript’s evolution:
- 1997 – Standardized as ECMAScript by Ecma International across browsers.
- 2005 – Ajax introduced asynchronous communication without page reloads.
- 2009 – Node.js allowed Javascript to run on servers.
- 2015 – ES6 added classes, promises, arrow functions.
- 2020 – Recent versions add useful features like optional chaining.
Today, Javascript is supported by all major browsers and is essential to front-end and full-stack development.
Javascript Syntax Basics
Javascript syntax includes:
- Semicolons to terminate statements.
- Comments using
//
or/* */
. - Dot notation like
obj.key
to access properties. - Declaring variables with
let
andconst
. - Functions defined with
function
or arrow syntax.
For example:
// Declare variable
let name = "John";
// Function definition
const greet = () => {
console.log("Hello " + name);
}
greet(); // Call function
Key Javascript Data Types
The main Javascript data types include:
- Strings – Text values defined in single or double quotes:
"hello"
. - Numbers – Numeric data like
10
or3.14
. - Booleans – Logical values
true
orfalse
. - Arrays – Ordered lists like
[1, 2, 3]
. - Objects – Key-value pairs like
{ "name": "John" }
.
Javascript is dynamically typed, so data types can change at runtime.
Variables and Constants
Variables store data during program execution. They are declared using let
and const
:
let age = 25; // Can be changed
const PI = 3.14; // Can't be changed
const
is preferred for values that won’t change.
Operators
Operators perform actions on values and variables. Javascript has:
- Assignment operators like
=
to assign values. - Arithmetic operators like
+
,-
,*
,/
for math. - Comparison operators like
==
,===
,>
,<
to compare values. - Logical operators like
&&
,||
,!
to combine conditions.
For example:
let a = 5;
let b = 10;
// Arithmetic
let c = a + b;
// Comparison
let d = a > 1; // true
Conditional Logic
Conditionals allow executing code based on meeting criteria. The main ones are:
if
statement – Runs code if condition is truthy.else if
– Chain additional conditions.else
– Default case.
For example:
let age = 17;
if (age >= 18) {
console.log("You are an adult");
} else if (age >= 13) {
console.log("You are a teenager");
} else {
console.log("You are a child");
}
This prints “You are a teenager”.
Loops
Loops repeat code until a condition is met. Javascript has:
for
loop – Executes code a set number of times.while
loop – Runs code until condition is falsy.for...of
loop – Iterates over array elements.
For example:
// For loop from 0 to 5
for (let i = 0; i < 5; i++) {
console.log(i);
}
// While condition is true
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
Functions
Functions are reusable blocks of code. They are defined using:
// Function declaration
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => {
return a + b;
}
Functions allow abstraction and help avoid code repetition.
Objects
Objects represent real-world entities with properties and methods:
let person = {
name: "Sarah",
age: 22,
greet: function() {
console.log(`Hello ${this.name}!`)
}
}
person.greet(); // Hello Sarah!
Objects organize data and functionality together.
Arrays
Arrays store ordered collections of data:
let fruits = ["Apple", "Orange", "Banana"];
fruits[0]; // Access first element - Apple
Arrays have useful built-in methods like .push()
, .pop()
, .shift()
etc.
Classes
ES6 introduced class syntax for more object-oriented code:
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello ${this.name}!`)
}
}
let person1 = new Person("John");
person1.greet(); // Hello John!
Classes provide a cleaner way to create objects and handle inheritance.
Asynchronous Programming
Asynchronous code runs in parallel using callbacks, promises and async/await. For example:
function fetchData(url) {
return new Promise((resolve, reject) => {
// Async operation
});
}
// Promise-based
fetchData('/api/users')
.then(data => console.log(data))
.catch(err => console.log(err));
// Async/await
async function fetch() {
try {
let data = await fetchData('/api/users');
console.log(data);
} catch (err) {
console.log(err);
}
}
Async techniques prevent blocking and improve overall efficiency.
Common Javascript Libraries and Frameworks
Some popular libraries and frameworks include:
- React – Frontend framework for building UI components.
- Vue – Progressive framework for building user interfaces.
- Angular – Framework for building single-page applications.
- jQuery – Library for DOM manipulation and events.
- Lodash – Utility library with helper functions.
- Moment – Library for date/time formatting and manipulation.
These provide additional functionality on top of vanilla Javascript.
Javascript Design Patterns
Key Javascript patterns include:
- Module – Encapsulates code into modules with public/private methods and state.
- Prototype – Creates objects based on a template prototype object.
- Observer – Subscribes to events/changes and reacts.
- Singleton – Restricts class instantiation to one object instance.
Patterns optimize code structure and improve maintainability.
Debugging Javascript
Debugging helps identify and fix bugs. Main techniques are:
console.log()
to log values.- Chrome DevTools for breakpoints and stepping through code.
try...catch
blocks to handle errors gracefully.- Linting with tools like ESLint.
- Using debugger statements like
debugger;
.
Sample Multiple Choice Questions
Here are a few sample MCQs to test your Javascript knowledge:
1. Which keyword is used to declare a variable in Javascript?
a) var
b) let
c) constant
d) int
Answer: b
Explanation: let is the modern way to declare variables with block scope in Javascript.
2. Which operator can be used to check for equality in values?
a) =
b) ==
c) !=
d) ===
Answer: d
Explanation: The strict equality operator === checks equality in both value and type.
3. Which loop can be used to iterate over array elements?
a) for
b) while
c) do-while
d) for…of
Answer: d
Explanation: The for…of loop allows iterating over arrays and other iterables.
Conclusion
This covers the core concepts and features of the Javascript language. Javascript has evolved significantly since its early days with the addition of classes, promises, async/await, and a host of other features. Robust frameworks have also been built on top of it.