Understanding JavaScript Basics and Features
Understanding JavaScript Basics and Features
(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
Popular versions:
JavaScript allows:
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)
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.
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.
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.
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)
Examples
let a = 10;
a = a + 5;
[Link](a);
2. Statement
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)
[Link]("Hello World");
2. Using [Link]()
[Link]("Welcome to JavaScript");
3. Using innerHTML
<p id="msg"></p>
<script>
[Link]("msg").innerHTML = "Hello User";
</script>
5. Using alert()
alert("Operation Successful");
6. Using confirm()
8. Using [Link]()
[Link]([1, 2, 3, 4]);
9. Using [Link]()
[Link]("This is a warning");
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.
If you must use the same quote inside, use backslash (\).
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>");
The process starts when the browser or [Link] receives JavaScript code.
let x = 10;
[Link](x);
3. Ignition Interpreter
A variable is a named memory location used to store data that can be used and changed
during program execution.
Definition
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
Definition
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 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
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.
var a;
[Link](a);
a = 10;
✔ Declaration hoisted
✔ Initialized as undefined
Reason
Reason
Same as let
Must be initialized at declaration
The time between entering the scope and variable declaration where accessing the variable
causes an error.
Q: Is let hoisted?
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
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
setTimeout(function () {
[Link]("Hello");
}, 1000);
7. Callback Function
function display(result) {
[Link](result);
}
8. Scope in Functions
9. Function Hoisting
sayHello();
function sayHello() {
[Link]("Hello");
}
A selector in JavaScript is used to select or access HTML elements from the webpage so
that we can read, change, or manipulate them.
1. getElementById()
[Link]("title");
2. getElementsByClassName()
[Link]("box");
[Link]("box")[0];
3. getElementsByTagName()
[Link]("p");
4. querySelector()
Selects the first matching element using CSS selectors.
[Link]("#title");
[Link](".box");
[Link]("p");
5. querySelectorAll()
[Link](".box");
6. Attribute Selector
[Link]("input[type='text']");
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
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
load
resize
scroll
[Link]("load", () => {
[Link]("Page loaded");
});
<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
Event Propagation
1. Event Bubbling
2. Event Capturing
[Link]();