Web Development Articles

Page 535 of 801

What happens when you do not declare a variable in JavaScript?

Ali
Ali
Updated on 15-Mar-2026 304 Views

When you don't declare a variable in JavaScript using var, let, or const, JavaScript automatically creates it as a global variable. This behavior can lead to unexpected results and is generally considered bad practice. How Undeclared Variables Work When you assign a value to an undeclared variable, JavaScript creates it in the global scope, even if you're inside a function. This happens because JavaScript searches the scope chain for the variable, and if not found, creates it globally. Example: Undeclared vs Declared Variables ...

Read More

Variable Hoisting in JavaScript

Rahul Sharma
Rahul Sharma
Updated on 15-Mar-2026 306 Views

When you can use a JavaScript variable before it is declared, it is done using a technique called hoisting. The parser reads through the complete function before running it. The behavior in which a variable appears to be used before it is declared is known as hoisting. JavaScript moves variable declarations to the top of their scope during compilation. Example: var Hoisting For example, the following code works due to hoisting: points = 200; var points; console.log(points); 200 The above works the same as if you had written: ...

Read More

What happens if we re-declare a variable in JavaScript?

Rahul Sharma
Rahul Sharma
Updated on 15-Mar-2026 264 Views

When you re-declare a variable in JavaScript using var, the original value is preserved. The re-declaration doesn't reset or change the variable's value. Example Let's see an example where we declare the variable age twice: Variable Re-declaration Example var age = 20; var age; // Re-declaration without assignment ...

Read More

What is a composite data type i.e. object in JavaScript?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 3K+ Views

A data type is known as a composite data type when it represents a number of similar or different data under a single declaration of variable i.e., a data type that has multiple values grouped together. There are mainly three types of composite data types named as below − Object Array Function In this article, we will discuss the first type of composite data type i.e. object. What is an Object? An object is a collection of properties i.e, an object can store the properties ...

Read More

What is the difference between Declaring and Initializing a variable in JavaScript?

Johar Ali
Johar Ali
Updated on 15-Mar-2026 584 Views

In JavaScript, declaring a variable means creating it in memory, while initializing means assigning it a value. Understanding this distinction is crucial for avoiding common bugs. What is Variable Declaration? Declaration creates a variable in the current scope and allocates memory for it. The variable exists but has no assigned value. var name; // Declaration only let age; // Declaration only const PI; // Error! const must be initialized SyntaxError: Missing ...

Read More

Can I use a JavaScript variable before it is declared?

Sravani Alamanda
Sravani Alamanda
Updated on 15-Mar-2026 3K+ Views

Yes, you can use a JavaScript variable before it is declared due to a mechanism called hoisting. However, the behavior differs significantly between var, let, and const. What is Hoisting? Hoisting is JavaScript's behavior where variable and function declarations are moved to the top of their scope during compilation. This means the JavaScript engine "sees" declarations before the code executes, but only declarations are hoisted—not initializations. Hoisting with var Variables declared with var are hoisted and initialized with undefined: var Hoisting Example ...

Read More

How to declare boolean variables in JavaScript?

Amit Sharma
Amit Sharma
Updated on 15-Mar-2026 712 Views

A boolean variable in JavaScript has two values: true or false. Boolean values are essential for conditional logic, comparisons, and control flow in JavaScript applications. Syntax let variableName = true; // or false let variableName = Boolean(value); // converts value to boolean Direct Boolean Assignment The simplest way to declare boolean variables is by directly assigning true or false: Boolean variable examples: Show Boolean Values ...

Read More

How to declare numbers in JavaScript?

Ali
Ali
Updated on 15-Mar-2026 5K+ Views

JavaScript is a dynamically typed language, meaning variables can hold any data type without explicit type declaration. You can declare number variables using var, let, or const keywords. Basic Number Declaration Here's how you can declare numbers in JavaScript: var points = 100; var rank = 5; let score = 85; const maxLevel = 10; console.log("Points:", points); console.log("Rank:", rank); console.log("Score:", score); console.log("Max Level:", maxLevel); Points: 100 Rank: 5 Score: 85 Max Level: 10 Different Number Types JavaScript supports integers, floating-point numbers, and special ...

Read More

How to write inline JavaScript code in HTML page?

Rahul Sharma
Rahul Sharma
Updated on 15-Mar-2026 783 Views

Inline JavaScript refers to JavaScript code written directly within an HTML document using tags, rather than linking to an external JavaScript file. This approach is useful for small scripts or page-specific functionality. Syntax To write inline JavaScript, place your code between opening and closing tags: // Your JavaScript code here Example: Basic Inline JavaScript Inline JavaScript Example Welcome to My Page Click Me ...

Read More

What are the differences between inline JavaScript and External file?

Alshifa Hasnain
Alshifa Hasnain
Updated on 15-Mar-2026 1K+ Views

JavaScript code can be included in HTML documents in two ways: inline JavaScript (embedded directly in HTML) and external JavaScript files (separate .js files). Understanding their differences helps choose the right approach for your project. Inline JavaScript Inline JavaScript refers to JavaScript code that is directly embedded within an HTML document, either inside tags or within HTML event attributes. Characteristics Scripts are executed immediately as the page loads Code loads instantly with the HTML - no additional HTTP requests The async and ...

Read More
Showing 5341–5350 of 8,010 articles
« Prev 1 533 534 535 536 537 801 Next »
Advertisements