-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
An array is a special variable that can hold multiple values at once, ordered and accessible by an index.
Unlike variables that hold a single value, arrays are containers for a list of values.
// Using array literals
let fruits = ["Apple", "Banana", "Cherry"];
// Using the Array constructor
let numbers = new Array(10, 20, 30);
// Creating an empty array
let empty = [];
Arrays are zero-indexed:fruits[0]
returns "Apple"
You can access, modify, or add elements using the index.
console.log(fruits[1]); // Banana
fruits[2] = "Mango"; // Change "Cherry" to "Mango"
Property/Method | Description | Example |
---|---|---|
length |
Returns number of elements | fruits.length → 3 |
push() |
Adds item at the end | fruits.push("Orange") |
pop() |
Removes last item | fruits.pop() |
shift() |
Removes first item | fruits.shift() |
unshift() |
Adds item at the beginning | fruits.unshift("Lemon") |
indexOf() |
Returns index of item or -1 | fruits.indexOf("Mango") |
includes() |
Checks if item exists | fruits.includes("Banana") |
toString() |
Converts array to comma-separated string | fruits.toString() |
join() |
Joins array elements with custom sep | fruits.join(" - ") |
let colors = ["Red", "Green", "Blue"];
console.log(colors.length); // 3
colors.push("Yellow"); // Add "Yellow"
console.log(colors[3]); // Yellow
colors.pop(); // Remove "Yellow"
console.log(colors.join(", ")); // Red, Green, Blue
Q1. How do you create an array of three fruits: Apple, Banana, and Mango?
Q2. What will colors[0]
return if colors = ["Red", "Green", "Blue"]
?
Q3. How do you add an item "Orange"
to the end of an array?
Q4. What method removes the last element from an array?
Q5. How do you check whether an item "Banana"
exists in an array?
Q6. What is the output of fruits.length
if fruits = ["Apple", "Banana"]
?
Q7. How do you remove the first element from an array?
Q8. Which method adds an element to the beginning of an array?
Q9. How do you convert an array into a string separated by dashes (-
)?
Q10. How do you find the index of "Mango"
in an array?
Q1: Which of the following creates an array in JavaScript?
Q2: What is the index of the first element in an array?
Q3: Which method adds an item at the end of an array?
Q4: What will ["a", "b", "c"].pop() return?
Q5: Which method removes the first element of an array?
Q6: Which of these methods checks if an item exists in an array?
Q7: What does join("-") do to an array ["a", "b", "c"]?
Q8: Which method adds an element to the start of the array?
Q9: What is the result of ["Apple", "Banana"].indexOf("Banana")?
Q10: What does the length property return for let arr = [1, 2, 3, 4];?