{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/javascript-objects-and-arrays-concepts","result":{"data":{"mdx":{"frontmatter":{"title":"JavaScript Objects and Arrays Concepts","category":"javascript","date":"June 23rd, 2020","readTime":3,"slug":"javascript-objects-and-arrays-concepts","embeddedImages":null,"image":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","backgroundColor":"#080808","images":{"fallback":{"src":"/static/f698eeb2c67124f6537f826c21a192fe/87706/p1.png","srcSet":"/static/f698eeb2c67124f6537f826c21a192fe/f80f8/p1.png 320w,\n/static/f698eeb2c67124f6537f826c21a192fe/9a63f/p1.png 640w,\n/static/f698eeb2c67124f6537f826c21a192fe/87706/p1.png 1280w","sizes":"(min-width: 1280px) 1280px, 100vw"},"sources":[{"srcSet":"/static/f698eeb2c67124f6537f826c21a192fe/c0bcc/p1.webp 320w,\n/static/f698eeb2c67124f6537f826c21a192fe/17574/p1.webp 640w,\n/static/f698eeb2c67124f6537f826c21a192fe/71d4d/p1.webp 1280w","type":"image/webp","sizes":"(min-width: 1280px) 1280px, 100vw"}]},"width":1280,"height":720}}}},"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"JavaScript Objects and Arrays Concepts\",\n  \"slug\": \"javascript-objects-and-arrays-concepts\",\n  \"image\": \"./images/p1.png\",\n  \"date\": \"2020-06-23T00:00:00.000Z\",\n  \"author\": \"john smith\",\n  \"category\": \"javascript\",\n  \"readTime\": 3\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h2\", null, \"Data Sets\"), mdx(\"p\", null, \"Imagine you have the following numbers: \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"2, 4, 6, 8, and 10\"), \". In order to represent this collection in memory (where a computer stores information), you need a way to represent it. There is the possibility of storing the numbers in a string like \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"\\\"2 4 6 8 10\\\"\"), \", but it would make it extremely difficult to access the numbers and perform operations on them. For example, if you wanted to sum the numbers in the collection, you would have to split the string by a space and then add up the numbers one by one.\"), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"JavaScript\"), \" provides an awesome data type that makes it very easy to work with groups of objects known as an Array. Arrays are written as lists of values surrounded by square brackets. The JavaScript representation of the collection above looks like this:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let list = [2, 4, 6, 7, 10]\\n\")), mdx(\"p\", null, \"As you may have noticed, \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"list\"), \" simply indicates the name we chose to use to store the array, so it doesn't matter what value you use. Now that we are using the list, JavaScript provides a convenient method for accessing the objects in the list. You use square brackets to locate a value in an array by its index. The term index is used to indicate the numerical position of a value in an array. Consider the following code:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let list = [2, 4, 6, 7, 10]\\n\\n// Logs 6 to the console\\nconsole.log(list[2])\\n\")), mdx(\"p\", null, \"The code above will construct our array of numbers, store it in a variable named \\\"list\\\" and then log the value at index \\\"2\\\" to the terminal. The comment indicates that it would log \\\"6\\\", and you may be assuming that the value at index \\\"2\\\" is 4. The reason \\\"6\\\" is logged is that arrays in JavaScript and many (but not all) other languages is \\\"zero-indexed\\\". This indicates that the first item in an array can be located at index 0. So the following code will log \\\"2\\\" to the output:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let list = [2, 4, 6, 7, 10]\\n\\n// Logs 2 to the console\\nconsole.log(list[0])\\n\")), mdx(\"p\", null, \"You can think of the index as the number of items to skip when starting at the first item in an array. It is also possible to put some logic in within the square brackets when handling array values. Take a look at the following code:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let list = [2, 4, 6, 7, 10]\\nlet index = 2\\nlet a = 5\\nlet b = -3\\n\\n// Logs 6 to the console\\nconsole.log(list[2])\\n\\n// Logs 6 to the console\\nconsole.log(list[1 + 1])\\n\\n// Logs 6 to the console\\nconsole.log(list[index])\\n\\n// Logs 6 to the console\\nconsole.log(list[a + b])\\n\")), mdx(\"p\", null, \"All of the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"console.log\"), \" codes above will result in the same thing being written to the console.\"), mdx(\"h2\", null, \"Properties\"), mdx(\"p\", null, \"Up to this point you have most likely been using a couple properties without actually understanding what they are and how they exist. For instance, if you have a string and would like to log it's length you could utilize code like the following:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let greeting = 'Hello World'\\n\\n// Logs 11 to the console\\nconsole.log(greeting.length)\\n\")), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \".length\"), \" on the end of \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"greeting\"), \" is used to access the length attribute that exists on all strings. Nearly every single JavaScript data type has an associated property. The exceptions to that policy are null and undefined. Trying to access values of those values will result in failures\"), mdx(\"p\", null, \"In JavaScript you can access the attributes of an object in two unique ways with a dot and with angle brackets. The following code will achieve the same thing:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let greeting = 'Hello World'\\nlet property = 'length'\\n\\n// Logs 11 to the console\\nconsole.log(greeting.length)\\n\\n// Logs 11 to the console\\nconsole.log(greeting['length'])\\n\\n// Logs 11 to the console\\nconsole.log(greeting[property])\\n\")), mdx(\"h2\", null, \"Methods\"), mdx(\"p\", null, \"Strings and Arrays both provide a length property. Furthermore, they both contain properties that are a function. These are referred to as \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"methods\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let greeting = 'Hello World'\\n\\n// Logs \\\"function\\\" to the console\\nconsole.log(typeof greeting.toUpperCase)\\n\\n// Logs \\\"HELLO WORLD\\\" to the console\\nconsole.log(greeting.toUpperCase())\\n\")), mdx(\"p\", null, \"It is highly suggested that you use dot notation when you know the property you are looking for. The bracket notation is a little more complicated, and should be avoided for when you need to use a dynamic property using a variable like in the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"greeting[property]\"), \" example above.\"), mdx(\"p\", null, \"In general, strings contain the same functions, and you can read more about them on javascript official doc. Now take a moment to look at the code above. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"greeting.toUpperCase()\"), \" calls the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"toUpperCase\"), \" function on all Strings, and without any parameters it is able to return \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"HELLO WORLD\"), \". Properties that contain methods are called methods for this purpose, they have access to the value to which they refer. This will be discussed in depth further on in this blog. In the meantime, know that when someone says \\\"call the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"[y]\"), \" function on \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"\\\"x\\\"\"), \" they are in fact saying that \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"\\\"x\\\"\"), \" has a value on it called \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"\\\"y\\\"\"), \" and it is a function.\"), mdx(\"p\", null, \"Arrays in JavaScript have some very useful methods for handling data. The following code demonstrates you just a couple of functions on arrays:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let list = [2, 4, 6, 7, 10]\\n\\nconsole.log(list)\\n// -> [2, 4, 6, 7, 10]\\n\\nlist.push(3)\\n\\nconsole.log(list)\\n// -> [2, 4, 6, 7, 10, 3];\\n\\nconsole.log(list.pop())\\n// -> 3\\n\\nconsole.log(list)\\n// -> [2, 4, 6, 7, 10]\\n\\nlist.unshift(3)\\n\\nconsole.log(list)\\n// -> [3, 2, 4, 6, 7, 10]\\n\\nconsole.log(list.shift())\\n// -> 3\\n\\nconsole.log(list)\\n// -> [2, 4, 6, 7, 10]\\n\\nconsole.log(list.concat([3, 4]))\\n// -> [2, 4, 6, 7, 10, 3, 4]\\n\\nconsole.log(list)\\n// -> [2, 4, 6, 7, 10]\\n\\nlist = list.concat([3, 4])\\n\\nconsole.log(list)\\n// -> [2, 4, 6, 7, 10, 3, 4]\\n\")), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"The \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"push\"), \" method inserts values to the end of an array\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"The \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"pop\"), \" operation removes the last value in an array and returns it\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"The \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"unshift\"), \" operation adds values to the beginning of an array\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"The \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"shift\"), \" operation removes the first value in an array and returns it\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"The \", mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"concat\"), \" method combines two arrays together and returns a new array (not changing either array) \", mdx(\"br\", null), mdx(\"br\", null))), mdx(\"p\", null, \"The names of the methods may be confusing/seem not intuitive. However, they are based on programming operations for stacks and queues. A stack is a common programming data structure that allows you to push and pop values in a Last In First Out (LIFO) pattern. A queue is a structure that uses a First In First Out (FIFO) pattern.\"), mdx(\"h2\", null, \"Objects\"), mdx(\"p\", null, \"Now you have a decent amount of experience with Strings and Arrays in JavaScript. However, what if you need to represent a set of arbitrary data. Say for example you have a blog post. The posts consists of a title, category, body, and a list of tags. It would be awfully difficult to represent a post using any of the data types you currently know. This is where objects come in. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Objects\"), \" are arbitary collections of properties. Representing the post as an object is fairly simple:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let post = {\\n  title: 'Welcome To My Blog',\\n  category: 'Announcements',\\n  body:\\n    'Hello and welcome to my blog. I will be posting about my adventures as a developer',\\n  tags: ['JS', 'CSS', 'HTML'],\\n}\\n\\nconsole.log(post.title)\\n// -> 'Welcome To My Blog'\\n\\nconsole.log(post.tags[1])\\n// -> 'CSS'\\n\")), mdx(\"p\", null, \"The above code organizes the post into a logical collection of properties. Note that it is possible to nest objects as properties of other objects, etc. In the above vode \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"tags\"), \" is an Array as a property on \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"post\"), \". The notation used above is called \\\"object literal\\\" notation. You will see further in this course that there is more than one way to create an object in JavaScript. Object literal notation starts with braces:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let myObject = null\\n\")), mdx(\"p\", null, \"Inside the braces there is a collection of properties separated by commas. Each property has a name followed by a colon and a value.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let myObject = {\\n    propA: 'value a',\\n    propB: 'value b'\\n    'prop c': 'value c'\\n};\\n\\n\")), mdx(\"p\", null, \"Notice above \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"'prop c'\"), \" is a property on \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"myObject\"), \", but it is not a valid binding name/number so it must be surrounded by quotes. It is common to write object properties on new lines, and indent them for better readability.\"), mdx(\"p\", null, \"Up to this point you have understood braces null to mean the start of a block of statements. Now we are introducing a new functionality in that they describe objects. While it may seem a little weird right now, in practice it's fairly easy to deal with both functionalities.\"), mdx(\"p\", null, \"You don't always have to define all the properties of an object up front, sometimes it can be useful to assign them later. You do this with an =, similar to how you declare and assign a variable:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let person = {\\n  firstName: 'John',\\n  lastName: 'Doe',\\n}\\n\\nconsole.log(person.email)\\n// -> undefined\\n\\nperson.email = 'john.doe@gmail.com'\\nconsole.log(person.email)\\n// -> 'john.doe@gmail.com'\\n\")), mdx(\"p\", null, \"Sometimes it can be useful to get all the properties that exist on an object, you can do this with \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Object.keys\"), \":\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let person = {\\n  firstName: 'John',\\n  lastName: 'Doe',\\n}\\n\\nconsole.log(Object.keys(person))\\n// -> ['firstName', 'lastName']\\n\")), mdx(\"p\", null, \"There is also an \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Object.assign\"), \" function that copies all properties from one object into another:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let person = {\\n  firstName: 'John',\\n  lastName: 'Doe',\\n}\\n\\nconsole.log(\\n  Object.assign(person, {\\n    firstName: 'Jane',\\n    email: 'jane.doe@gmail.com',\\n  })\\n)\\n// -> { firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com' }\\n\")), mdx(\"p\", null, \"This isn't the only way to change properties on an object, you can update an object's properties with the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"=\"), \" operator:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let person = {\\n  firstName: 'John',\\n  lastName: 'Doe',\\n}\\n\\nperson.firstName = 'Jane'\\n\\nconsole.log(person)\\n// -> { firstName: 'Jane', lastName: 'Doe' }\\n\")), mdx(\"p\", null, \"Now that you know what Objects are, it is not a stretch to understand that Arrays are just a special kind of object used for storing sequences of things. In fact, Strings are also special types of objects used to store sequences of characters. So in a way Strings are like Arrays.\"), mdx(\"h2\", null, \"Object Mutability\"), mdx(\"p\", null, \"You have observed up to this point that values in an object can be modified. Prior to now you have been dealing mostly with objects that cannot be modified, such as numbers and strings. Those are called \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"\\\"immutable\\\"\"), \" objects. It is not possible to change \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"1 to 2\"), \". Yes you can do \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"let x = 1; x = 2\"), \", but that doesn't change \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"1 to 2\"), \", it simply changes \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x to 2\"), \". Objects are different. You can change the properties on an object, causing it to have different content at different times. Consider the following code:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"let a = {\\n  value: 10,\\n}\\n\\nlet b = a\\nlet c = {\\n  value: 10,\\n}\\n\\nconsole.log(a == b)\\n// -> true\\nconsole.log(a === b)\\n// -> true\\nconsole.log(a == c)\\n// -> false\\nconsole.log(a === c)\\n// -> false\\n\\na.value = 15\\n\\nconsole.log(a.value)\\n// -> 15\\nconsole.log(b.value)\\n// -> 15\\nconsole.log(c.value)\\n// -> 10\\n\\na = {\\n  value: 10,\\n}\\n\\nconsole.log(a.value)\\n// -> 10\\nconsole.log(b.value)\\n// -> 15\\nconsole.log(c.value)\\n// -> 10\\n\")), mdx(\"p\", null, \"In the above example \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"a\"), \" and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"b\"), \" are the same object, so changing \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"a\"), \" results in a change to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"b\"), \" as well. However setting a to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"a\"), \" new object disconnects it from the object that is assigned to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"b\"), \".\"), mdx(Link, {\n    to: \"/posts\",\n    className: \"btn center-btn\",\n    mdxType: \"Link\"\n  }, \"all posts\"));\n}\n;\nMDXContent.isMDXComponent = true;","id":"13ce42b3-d53f-5bca-b792-86434c1f9027"}},"pageContext":{"slug":"javascript-objects-and-arrays-concepts"}},"staticQueryHashes":["3361116154","3824322444"]}