The JavaScript array size is returned by the length property. Write array.length to get an integer equal to the number of elements. It updates automatically when items are added or removed. There is no separate size() function in vanilla JavaScript. To count only defined, truthy values, use array.filter(Boolean).length.
JavaScript Array Size, indexOf and Data Types: What You Actually Need
- Array size: always use
array.length, a live property, not a function call. - indexOf: returns the zero-based index of the first match, or
-1if the value is not found. - Non-primitive types: objects, arrays and functions are stored by reference, not by value.
- Query strings:
URLSearchParamsis the modern, clean way to read URL parameters in the browser. - getAttribute vs property:
getAttributereads the original HTML attribute; direct property access reads the current live DOM state.
JavaScript is consistently ranked among the top three most-used programming languages worldwide. According to the Stack Overflow Developer Survey 2024, 65.4% of professional developers use JavaScript, making it the most popular language for the twelfth consecutive year. If you are a student or a fresher in India targeting a front-end or full-stack role, these fundamentals are not optional.
How to Get the JavaScript Array Size
Every array in JavaScript has a built-in length property. You do not call it like a function. You access it like this:
const fruits = ['mango', 'banana', 'guava']; console.log(fruits.length); // 3
The javascript array size property is writable, which catches beginners off guard. If you set fruits.length = 1, the array is truncated to just ['mango']. If you set it to a number larger than the current length, the array gains empty slots. This behaviour is intentional and documented in the ECMAScript specification, but it surprises almost every new learner.
Sparse arrays are another common gotcha. const a = [1,,3] has a length of 3, but the middle element is undefined. The javascript array size counts slots, not defined values. Use array.filter(Boolean).length if you want a count of truthy, defined values only.
JavaScript Array Length vs Size: Key Difference
Some languages like Java and Python have a size() method or len() function. JavaScript has neither for arrays. The javascript array size is always accessed via array.length. Calling array.size() throws a TypeError. This is one of the most common mistakes developers make when switching from another language to JavaScript.
How to Check if a JavaScript Array Is Empty
Use array.length === 0 to check if an array is empty. This is the fastest and most readable approach. Avoid converting the array to a string or using JSON.stringify for this check.
How indexOf Works in JavaScript
indexOf scans an array or string from left to right and returns the zero-based index of the first exact match. If nothing matches, it returns -1. That -1 return is the most important thing to remember about indexOf in JavaScript, because a common bug is treating any truthy return as a match.
The comparison uses strict equality (===), so ['1', 2, 3].indexOf(1) returns -1, not 0. The number 1 and the string '1' are different types. This trips up developers coming from PHP or Python where type coercion is more forgiving.
For strings, indexOf works the same way: 'javascript'.indexOf('script') returns 4. You can also pass a second argument as a start position: 'banana'.indexOf('a', 2) returns 3, skipping the first ‘a’ at index 1.
When you need to check for any match without caring about position, array.includes(value) is more readable than checking indexOf !== -1. Use the right tool for the job.
Primitive vs Non-Primitive Data Types in JavaScript
This distinction comes up in every serious JavaScript interview. Getting it wrong on a placement test at companies like Infosys, TCS Digital or a product startup will cost you the round.
Primitive Types
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol and null. They are stored directly in the variable. When you copy a primitive, you get a completely independent copy. Changing one does not affect the other.
let a = 10; let b = a; b = 20; console.log(a); // still 10
Non-Primitive Data Types
Non-primitive types, also called reference types, include objects, arrays and functions. When you assign a non-primitive to a variable, you are storing a reference to a memory address, not the actual data. Two variables can point to the same object, and changing one changes what the other sees.
const obj1 = {score: 100}; const obj2 = obj1; obj2.score = 200; console.log(obj1.score); // 200
Arrays in JavaScript are technically objects. typeof [] returns 'object', which surprises most beginners. Use Array.isArray(value) to check if something is actually an array. This also means that when you check the javascript array size using length, you are accessing an object property, not calling a method.
According to the MDN Web Docs JavaScript Guide (2024), the reference-vs-value distinction is one of the most common sources of bugs in beginner and intermediate JavaScript code. Understanding it properly separates candidates who pass coding rounds from those who do not.
Primitive vs Non-Primitive: Quick Comparison
| Feature | Primitive | Non-Primitive (Reference) |
|---|---|---|
| Examples | string, number, boolean, null, undefined, symbol, bigint | Object, Array, Function |
| Storage | Stack (by value) | Heap (by reference) |
| Copy behaviour | Independent copy | Shared reference |
| Mutability | Immutable (value cannot change in place) | Mutable |
| typeof result | Returns the type name | Returns ‘object’ (or ‘function’) |
| Equality check | Compares value | Compares memory address |
| Array size check | N/A | Use array.length (object property) |
Query Strings and the DOM
Reading a Query Parameter in JavaScript
A URL like https://example.com/results?score=95&name=Priya contains a query string after the ?. The modern way to read these values is URLSearchParams. It is supported in all major browsers and does not require any library.
const params = new URLSearchParams(window.location.search); const score = params.get('score'); // '95'
The returned value is always a string, so convert it with Number(score) or parseInt(score, 10) if you need arithmetic. Older codebases parse query strings manually with split and indexOf, which is error-prone. Write URLSearchParams in any new code.
getAttribute vs Direct Property Access
getAttribute('href') returns exactly what is written in the HTML. If the HTML says href="/about", you get the string "/about". Direct property access via element.href returns the fully resolved URL, like "https://www.example.com/about".
The same logic applies to boolean attributes like disabled. getAttribute('disabled') returns an empty string or the literal attribute value; element.disabled returns a boolean true or false.
JavaScript Skills and Career Opportunities in India
According to NASSCOM’s India Tech Sector Report 2024, demand for JavaScript-skilled developers in India grew by 22% year-on-year, with the highest concentration of roles in Bengaluru, Hyderabad and Pune. Freshers with demonstrable JavaScript skills, including DOM manipulation and async patterns, are consistently shortlisted faster than those without a portfolio.
If you are planning to build a career around front-end development, DOM manipulation and JavaScript array methods are skills you will use every single day. Check out the top skills every college student must learn before graduation to see how JavaScript fits into a broader career-ready skill set.
Learning these concepts from structured courses matters more than most students realise. The best free online learning platforms for students in 2025 include options that take you from array basics all the way to full-stack projects. Pair that with the top online courses for high-salary jobs in 2025 and you have a clear learning path.
If you are wondering which direction to take your career after picking up these skills, the guide on how to choose the right career path after graduation in 2026 is worth reading before you commit to a specialisation. If you are actively job hunting, the roundup of entry-level jobs for teens and graduates lists roles where JavaScript knowledge directly helps you get hired.
JavaScript fundamentals like the javascript array size property, indexOf behaviour and the primitive vs non-primitive distinction appear in technical screening rounds at companies across India, from TCS and Wipro to funded product startups. Practise these in your browser console daily. Build small projects. Then move on to async JavaScript, promises and the Fetch API.
For a broader view of where the industry is heading, the best career options in 2026 for students and professionals covers the roles where JavaScript skills translate into real salary growth. When you are ready to go beyond fundamentals, 3University’s JavaScript and web development certification courses in Cybersecurity, Ethical Hacking, AI, Blockchain and Web3 give you practical, industry-aligned skills that employers actually test for.
Frequently Asked Questions
How do you get the size of a JavaScript array?
Use the length property: array.length. It is a live property, not a method, so do not add parentheses. The javascript array size returned includes empty slots in sparse arrays. If you want a count of only defined, truthy values, use array.filter(Boolean).length instead.
What is the difference between JavaScript array length and size?
JavaScript arrays do not have a size() method. The correct way to get the javascript array size is array.length. Calling array.size() throws a TypeError. The length property counts all slots including empty ones in sparse arrays, not just defined values.
How do you check if a JavaScript array is empty?
Use array.length === 0. This returns true if the array has no elements and false otherwise. It is the fastest and most readable way to check for an empty array in JavaScript without converting it to another type.
What does indexOf return when a value is missing?
indexOf returns -1 when the value is not found. It uses strict equality (===), so type mismatches cause a miss. Searching for the number 1 in an array containing the string '1' returns -1. Always check the type of what you are searching for before calling indexOf.
What is a non-primitive data type in JavaScript?
A non-primitive type, also called a reference type, stores a reference to a memory location rather than the actual value. Objects, arrays and functions are all non-primitive. Copying a non-primitive variable copies the reference, not the data, so both variables point to the same underlying object.
How do you read a query parameter in JavaScript?
Use URLSearchParams: new URLSearchParams(window.location.search).get('paramName'). This returns the value as a string, or null if the parameter does not exist. It handles encoding, multiple values and edge cases cleanly. Avoid manual string splitting with split('&') in new code as it breaks on encoded characters.
What is the difference between getAttribute and property access in JavaScript?
getAttribute('href') returns the raw string as written in the HTML source. Direct property access like element.href returns the browser-resolved, absolute URL. For boolean attributes like disabled, getAttribute returns a string while the property returns a boolean. Use getAttribute when you need the authored value and the property when you need the computed state.
Last updated: June 2025. Reviewed by the 3University editorial team.