Objects in javascript

creating and using an object in javascript, the full version of this testing is on codepen

Javascript

// creating an object
var anObject = {
  name: "this is an object",
  "object number": 2,
  more_strings: 3,
  "object array": ["one", "two"]
};
// is this an object?
console.log("anObject is a " + typeof anObject);
console.log("anObject length " + anObject.length); // objects dont have length

var anArray = [ ["i am an array"],["i am an array"] ];
console.log("anArray is a " + typeof anArray);
// this will also show that this is an object

console.log("anArray length " + anArray.length); // arrays do have length but are also type of object

// accessing the object values
var object_name = anObject.name;
console.log(object_name);

var object_number = anObject["object number"]; // to access the object with the string as a name you need to use the brackets similar to an array. 
console.log(object_number);

// access it via a dynamic variable name
var obj_var_name = "more_strings";
var more_strings_val = anObject[obj_var_name];
console.log("more_strings_val: "+more_strings_val);

Demo

External Link for Objects in javascript

Other Items in javascript
Page Views

This page has been viewed 52 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹