Object Oriented Programming Articles

Page 108 of 589

Disabling arrow key in text area using JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 375 Views

You can disable arrow key navigation in a text area using JavaScript by intercepting keydown events and preventing their default behavior. This technique is useful when you want to restrict user interaction or create custom navigation controls. Understanding Arrow Key Codes Arrow keys have specific key codes that we can detect: Left Arrow: 37 Up Arrow: 38 Right Arrow: 39 Down Arrow: 40 Complete Example Disable Arrow Keys in Textarea ...

Read More

Validating a file size in JavaScript while uploading

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

File size validation is essential for web applications to prevent users from uploading files that are too large for your server or application limits. JavaScript provides built-in methods to check file size before upload begins. How File Size Validation Works When a user selects a file using an HTML file input, JavaScript can access the file's properties through the files property. The size property returns the file size in bytes, which we can then validate against our requirements. Basic File Size Validation ...

Read More

Undeclared vs Undefined? In JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In JavaScript, undeclared and undefined are two different concepts that developers often confuse. Understanding the distinction is crucial for debugging and writing reliable code. Key Differences Undeclared occurs when you try to access a variable that hasn't been declared using var, let, or const. This results in a ReferenceError. Undefined occurs when a variable has been declared but hasn't been assigned a value. The variable exists but contains the special value undefined. Example: Undefined Variable Undefined Variable ...

Read More

innerHTML vs innerText in JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

The innerHTML and innerText properties are two different ways to access and manipulate the content of HTML elements in JavaScript. innerHTML - Returns or sets the HTML markup inside an element, including all tags, formatting, and spacing. It preserves the complete HTML structure. innerText - Returns or sets only the visible text content, stripping out all HTML tags and normalizing whitespace. Key Differences Property HTML Tags Whitespace Hidden Elements innerHTML Preserved Preserved Included innerText Removed Normalized Excluded Example ...

Read More

Reduce sum of digits recursively down to a one-digit number JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

We have to write a function that takes in a number and keeps adding its digits until the result is a one-digit number. When we have a one-digit number, we return it. The code uses a recursive function that keeps adding digits until the number is between -9 and 9. We handle the sign separately to avoid duplicating logic. How It Works The algorithm follows these steps: Take the absolute value of the number to handle sign separately If the number is greater than 9, split it into digits and sum them Recursively call the ...

Read More

How do JavaScript primitive/object types passed in functions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 286 Views

In JavaScript, understanding how data types are passed to functions is crucial. Primitive types are passed by value, while objects are passed by reference. This affects how modifications inside functions impact the original data. Pass by Value (Primitives) Primitive types (string, number, boolean, null, undefined, symbol, bigint) are passed by value. A copy is made, so changes inside the function don't affect the original variable. Pass by Value Example function modifyPrimitive(value) { value = value + 10; document.getElementById('output').innerHTML += ...

Read More

How to compare two arrays in JavaScript and make a new one of true and false? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 530 Views

We have 2 arrays in JavaScript and we want to compare one with the other to see if the elements of master array exists in keys array, and then make one new array of the same length that of the master array but containing only true and false (being true for the values that exists in keys array and false the ones that don't). Let's say, if the two arrays are − const master = [3, 9, 11, 2, 20]; const keys = [1, 2, 3]; Then the final array should be − ...

Read More

Escape characters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 9K+ Views

Escape characters are special characters that require a backslash (\) prefix to be displayed literally in JavaScript strings. Without escaping, these characters have special meanings that could break your code or produce unexpected results. Common Escape Characters Code Result Description ' Single quote Allows single quotes inside single-quoted strings " Double quote Allows double quotes inside double-quoted strings \ Backslash Displays a literal backslash character New Line Creates a line break \t Tab Creates horizontal spacing \r Carriage Return Returns ...

Read More

How to sum all elements in a nested array? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Let's say, we are supposed to write a function that takes in a nested array of Numbers and returns the sum of all the numbers. We are required to do this without using the Array.prototype.flat() method. Let's write the code for this function − Using Recursive Approach const arr = [ 5, 7, [ 4, [2], 8, [1, 3], 2 ], [ 9, [] ], 1, 8 ]; const findNestedSum = ...

Read More

How to borrow methods in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

Method borrowing allows an object to use another object's method by temporarily setting the context using call(), apply(), or bind(). Understanding Method Borrowing In JavaScript, methods are just functions attached to objects. When you borrow a method, you're calling a function from one object but executing it in the context of another object using this binding. Using call() Method The call() method invokes a function with a specified this context and individual arguments. Method Borrowing with call() ...

Read More
Showing 1071–1080 of 5,881 articles
« Prev 1 106 107 108 109 110 589 Next »
Advertisements