JavaScript Associative Array
You are Here:
JavaScript Associative Array
An array with named indexes are called Associative Array.
JavaScript does not have the concept of Associative Array. But it treat them as an object (as array itself an object).
Using Associative Array
Tips: It is not recommended to have this kind of key/value pair instead use the object directly.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var bike = [];
// bike[key] = value;
bike["company"] = "Honda";
bike["model"] = "CBR 1000RR";
bike["color"] = "Red";
document.write("I bought " +bike["model"]);
</script>
</body>
</html>
Using Object
The following example is an alternate way for the previous example.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var bike = {
company: "Honda",
model: "CBR 1000RR",
color: "red"
};
document.write("I bought " +bike.model);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.