0% found this document useful (0 votes)
45 views16 pages

Understanding JavaScript Basics and Features

JavaScript is a high-level, interpreted programming language primarily used for creating interactive web pages. It was developed in 1995 by Brendan Eich and has evolved through various versions, with ES6 being a significant update. JavaScript features include dynamic typing, event-driven programming, and the ability to run on both client and server sides, making it essential for modern web development.

Uploaded by

deva maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views16 pages

Understanding JavaScript Basics and Features

JavaScript is a high-level, interpreted programming language primarily used for creating interactive web pages. It was developed in 1995 by Brendan Eich and has evolved through various versions, with ES6 being a significant update. JavaScript features include dynamic typing, event-driven programming, and the ability to run on both client and server sides, making it essential for modern web development.

Uploaded by

deva maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.1 what is JavaScript?

(Detailed Explanation)
JavaScript (JS) is a high-level, interpreted, lightweight programming language mainly
used to make web pages interactive and dynamic. It is one of the core technologies of the
web, along with HTML (structure) and CSS (style).

HTML → Structure
CSS → Design
JavaScript → Behavior & Logic

History of JavaScript

 Created in 1995 by Brendan Eich


 Developed at Netscape
 Initially called Mocha, then LiveScript, finally JavaScript
 Standardized as ECMAScript (ES)

Popular versions:

 ES5 (2009) – Widely supported


 ES6 / ES2015 – Major update (let, const, classes, arrow functions)
 Latest ES versions – Updated yearly

Q.2 Why JavaScript is Important

JavaScript allows:

 Dynamic content updates without page reload


 Form validation
 Interactive UI (menus, sliders, popups)
 Communication with servers (AJAX / Fetch API)
 Building full-stack applications

Q.3 Features of JavaScript

1. Lightweight Programming Language

JavaScript is a lightweight language because it requires very little memory and system
resources. It runs directly inside the browser without the need for complex setups or heavy
libraries, making web pages load faster and perform efficiently.

2. Interpreted Language

JavaScript is an interpreted language, which means the code is executed line by line by the
JavaScript engine at runtime. There is no separate compilation step, allowing faster
development and easier debugging.
3. Platform Independent

JavaScript is platform independent. Any system that has a web browser can execute
JavaScript code, regardless of the operating system (Windows, Linux, macOS, Android, etc.).

4. Object-Based (Prototype-Oriented)

JavaScript is an object-based language that uses prototype-based inheritance. Objects can


inherit properties and methods from other objects, making code reusable and flexible.

5. Dynamic Typing

JavaScript is dynamically typed, meaning variable types are determined at runtime. The
same variable can store different types of values during execution, which increases flexibility
but requires careful coding.

6. Event-Driven Programming

JavaScript follows an event-driven model, where code execution is triggered by events such
as mouse clicks, key presses, page loading, or form submissions. This makes web pages
interactive.

7. Client-Side Execution

JavaScript runs mainly on the client side, meaning code executes in the user’s browser. This
reduces server load and improves application performance by handling tasks like form
validation and UI updates locally.

8. Server-Side Capability

With the introduction of [Link], JavaScript can also be used on the server side. This allows
developers to build full-stack applications using a single language.

9. Asynchronous and Non-Blocking

JavaScript supports asynchronous programming, allowing long-running tasks (such as API


calls or file operations) to run in the background without blocking the main thread. This
improves responsiveness and performance.

10. DOM Manipulation

JavaScript can interact with and modify the Document Object Model (DOM). This allows
dynamic changes to web pages such as updating content, styles, and structure without
reloading the page.

11. High Performance

Modern JavaScript engines (like Google’s V8) use Just-In-Time (JIT) compilation, which
converts JavaScript into machine code during execution, resulting in faster performance.
12. Secure Language

JavaScript follows strict security rules in browsers, such as sandboxing and same-origin
policy, which prevent unauthorized access to user data and system resources.

Q.4. JavaScript Execution Environments

1. Browser
o Chrome (V8 Engine)
o Firefox (SpiderMonkey)
o Edge (Chakra)
2. Server
o [Link]
3. Other Platforms
o React Native (mobile)
o Electron (desktop)

Q.5 what is a Statement and an Instruction?


1. Instruction

An instruction is a single command given to the computer to perform a specific task.


It tells the computer what action to do.

Examples

let a = 10;
a = a + 5;
[Link](a);

Each line above is an instruction because it performs one action.

2. Statement

A statement is a complete line or block of code that performs a specific operation.


It may contain one or more instructions..

Examples

if (a > 10) {
[Link]("Greater");
}

The entire if block is a statement, even though it contains multiple instructions inside it.
Q.6 Output Possibilities in JavaScript
1. Using [Link]() (Most Common)

Used mainly for debugging and development.

[Link]("Hello World");

2. Using [Link]()

Writes output directly to the HTML page.

[Link]("Welcome to JavaScript");

3. Using innerHTML

Displays output inside an HTML element.

<p id="msg"></p>
<script>
[Link]("msg").innerHTML = "Hello User";
</script>

4. Using textContent / innerText

Displays plain text only.

[Link]("msg").textContent = "Hello JavaScript";

✔ Safer than innerHTML


✔ No HTML parsing

5. Using alert()

Displays output in a popup dialog box.

alert("Operation Successful");

6. Using confirm()

Displays output with OK / Cancel buttons.

confirm("Are you sure?");


7. Using prompt()

Takes input and shows output.

let name = prompt("Enter your name");


alert("Hello " + name);

✔ Input + output combined

8. Using [Link]()

Displays data in table format.

[Link]([1, 2, 3, 4]);

✔ Useful for arrays and objects

9. Using [Link]()

Displays error messages.

[Link]("Something went wrong");

✔ Used for error output

10. Using [Link]()

Displays warning messages.

[Link]("This is a warning");

✔ Shows warning in console

Q. 7 Using Single Quotes and Double Quotes in JavaScript

In JavaScript, strings can be written using:

 Single quotes ' '


 Double quotes " "

Rule

If you start a string with double quotes, use single quotes inside
it.
If you start a string with single quotes, use double quotes inside
it.
This avoids confusion and syntax errors.

1. Double Quotes Outside, Single Quotes Inside


[Link]("JavaScript is a 'programming' language");

2. Single Quotes Outside, Double Quotes Inside


[Link]('JavaScript is a "programming" language');

3. Using Escape Character (\)

If you must use the same quote inside, use backslash (\).

[Link]("JavaScript is \"easy\" language");


[Link]('JavaScript is \'easy\' language');

4. Using Template Literals (Best Method)

JavaScript also supports backticks (`).

[Link](`JavaScript is "easy" and 'powerful'`);

Q.8 Can [Link]() apply both HTML and CSS?

Yes, [Link]() can write both HTML and CSS code, but it is not recommended for
modern web development because it overwrites the document and affects performance.

Example :

[Link]("<h1>Hello JavaScript</h1>");

[Link]("<p>This is paragraph</p>");

[Link]("<h2 style='color:red;'>Red Text</h2>");

Q.9 What is V8 Engine?

The V8 Engine is a high-performance JavaScript engine developed by Google.


It is used in Google Chrome and [Link] to execute JavaScript code.

Purpose: Convert JavaScript code into fast, optimized machine code.


V8 Engine Execution Process (Step by Step)
1. JavaScript Source Code

The process starts when the browser or [Link] receives JavaScript code.

let x = 10;
[Link](x);

2. Parsing (Syntax Analysis)

 V8 reads the JavaScript code


 Checks for syntax errors
 Converts code into an Abstract Syntax Tree (AST)

✔ If syntax error → execution stops

3. Ignition Interpreter

 AST is converted into bytecode


 Bytecode is executed by Ignition, V8’s interpreter
 Execution starts immediately (fast startup)

✔ Good for short-running scripts

4. Profiling (Monitoring Execution)

 V8 monitors code execution


 Detects hot code (code executed many times)
 Collects type and performance data

5. TurboFan Compiler (JIT Compilation)

 Hot code is sent to TurboFan


 TurboFan converts bytecode into optimized machine code
 Uses Just-In-Time (JIT) compilation

✔ Improves performance dramatically

6. Optimized Code Execution

 Machine code runs directly on CPU


 Faster than interpreted code

Q.10 Is JavaScript Interpreted or Compiled?


JavaScript is a JIT (Just-In-Time) compiled language that uses both interpretation and
compilation.
JavaScript is not purely interpreted or compiled.
It uses Just-In-Time (JIT) compilation, combining interpretation and compilation for better
performance.

Q.11 what is a Variable?

A variable is a named memory location used to store data that can be used and changed
during program execution.

In simple words: A variable stores values like numbers, text, or objects.

How to Declare Variables in JavaScript

JavaScript provides three keywords to declare variables:

1. var (Old Method)

Definition

var is the old way of declaring variables in JavaScript.

var x = 10;
Characteristics

 Function-scoped
 Can be redeclared
 Can be updated
 Hoisted (initialized as undefined)

var a = 10;
var a = 20; // allowed
⚠ Not recommended in modern JavaScript

2. let (Modern Method)

Definition

let allows you to declare block-scoped variables.

let y = 20;
Characteristics

 Block-scoped
 Cannot be redeclared in the same scope
 Can be updated
 Hoisted but not initialized

let b = 10;
b = 20; // allowed
3. const (Constant Variable)

Definition

const is used to declare constant values.

const z = 30;
Characteristics

 Block-scoped
 Cannot be redeclared
 Cannot be reassigned
 Must be initialized

const PI = 3.14;
// PI = 5; ❌ Error
✔ Object values can be modified

const user = { name: "Dev" };


[Link] = "Raj"; // allowed

Scope of Variables

1. Global Scope

let x = 10;
Accessible everywhere.

2. Function Scope

function test() {
var a = 5;
}
Accessible only inside the function.

3. Block Scope

if (true) {
let b = 10;
}
Accessible only inside the block.

Q.12 What is Variable Hoisting?


Hoisting is a JavaScript mechanism where variable declarations are moved to the top of
their scope before execution.

Important: Only the declaration is hoisted, not the initialization.

Hoisting with var


[Link](a); // undefined
var a = 10;

Why does this work?

JavaScript internally treats it like this:

var a;
[Link](a);
a = 10;

✔ Declaration hoisted
✔ Initialized as undefined

Hoisting with let


[Link](b); // ❌ ReferenceError
let b = 20;

Reason

 let is hoisted but not initialized


 Exists in Temporal Dead Zone (TDZ) until declared

Hoisting with const


[Link](c); // ❌ ReferenceError
const c = 30;

Reason

 Same as let
 Must be initialized at declaration

Temporal Dead Zone (TDZ)

The time between entering the scope and variable declaration where accessing the variable
causes an error.

Hoisting in Function Scope


function test() {
[Link](x); // undefined
var x = 5;
}

Hoisting Comparison Table


Keywor Hoisted Initialized Access Before Declaration
d
var Yes Yes (undefined) ✅ Allowed
let Yes No ❌ Error
const Yes No ❌ Error

Common Interview Question

Q: Is let hoisted?

✔ Yes, but it is in the Temporal Dead Zone

Q.13 What is a Function?

A function in JavaScript is a block of reusable code designed to perform a specific task.


It runs only when it is called (invoked).

Why Functions Are Used

 Code reusability
 Modularity
 Easy maintenance
 Reduces code size
 Improves clarity

Syntax of a Function
function functionName(parameters) {
// code to be executed
return value;
}

1. Function Declaration

function add(a, b) {
return a + b;
}
✔ Hoisted
✔ Can be called before declaration

2. Function Expression
const sum = function (a, b) {
return a + b;
};
❌ Not hoisted
✔ Stored in a variable

3. Arrow Function (ES6)

const multiply = (a, b) => a * b;


✔ Short syntax
❌ No this binding
❌ Not hoisted

4. Function with Parameters and Arguments

function greet(name) {
[Link]("Hello " + name);
}

greet("Dev"); // argument
5. Return Statement

function square(x) {
return x * x;
}
✔ Sends value back
✔ Function stops after return

6. Anonymous Function

Function without a name.

setTimeout(function () {
[Link]("Hello");
}, 1000);
7. Callback Function

A function passed as an argument.

function display(result) {
[Link](result);
}

function add(a, b, callback) {


callback(a + b);
}

8. Scope in Functions

Variables declared inside a function are local.


function test() {
let x = 10;
}

9. Function Hoisting

Only function declarations are hoisted.

sayHello();

function sayHello() {
[Link]("Hello");
}

Q.14 What is a Selector in JavaScript?

A selector in JavaScript is used to select or access HTML elements from the webpage so
that we can read, change, or manipulate them.

In simple words: Selectors help JavaScript find HTML elements.

Types of Selectors in JavaScript

JavaScript uses DOM selectors to access elements.

1. getElementById()

Selects one element using its id.

[Link]("title");

2. getElementsByClassName()

Selects multiple elements using a class name.

[Link]("box");
[Link]("box")[0];

3. getElementsByTagName()

Selects elements by HTML tag name.

[Link]("p");

4. querySelector()
Selects the first matching element using CSS selectors.

[Link]("#title");
[Link](".box");
[Link]("p");

5. querySelectorAll()

Selects all matching elements.

[Link](".box");

6. Attribute Selector

[Link]("input[type='text']");

Q.15What is an Event in JavaScript?

An event in JavaScript is an action or occurrence that happens in the browser and can be
detected and handled by JavaScript.

In simple words:
An event occurs when a user or browser does something.

Examples of Events

 Clicking a button
 Moving the mouse
 Pressing a key
 Loading a webpage
 Submitting a form

Why Events Are Important

 Make web pages interactive


 Respond to user actions
 Control application flow
 Improve user experience

Types of Events in JavaScript


1. Mouse Events

 click
 dblclick
 mouseover
 mouseout

[Link]("click", function () {
alert("Button clicked");
});

2. Keyboard Events

 keydown
 keyup
 keypress

[Link]("keydown", () => {
[Link]("Key pressed");
});

3. Form Events

 submit
 change
 focus
 blur

[Link]("submit", function (e) {


[Link]();
});

4. Window / Document Events

 load
 resize
 scroll

[Link]("load", () => {
[Link]("Page loaded");
});

Ways to Handle Events

1. Inline Event Handling (Not Recommended)

<button onclick="show()">Click</button>
2. DOM Property Method

[Link] = function () {
alert("Clicked");
};

3. addEventListener() (Recommended)

[Link]("click", show);

function show() {
alert("Clicked");
}
✔ Allows multiple events
✔ Clean and professional

Event Object

The event handler receives an event object.

[Link]("click", function (e) {


[Link]([Link]);
});

Event Propagation

1. Event Bubbling

Event moves from child → parent.

2. Event Capturing

Event moves from parent → child.

[Link]("click", handler, true); // capturing

Prevent Default Behavior

[Link]();

You might also like