The language also tries to help by silently converting value types.
By definition JavaScript is an interpreted and dynamic language and it requires that the type system works during the code execution. Not to mention all the times that we mistake the value type of this and try forbidden things on it.Ī good type system helps us avoid these common mistakes. Keep in mind that there aren't constructors like Undefined or Null to save us here.
Since Javascript cannot locate the property it returns its default fallback value: undefined.Īnother common type-related mistake is when we try to change or access a property from a value that is null or undefined. Have you ever called a function and received the error undefined is not a function? This happens when we try to access a property that is not defined. For example, when we try to multiply a number by a string we get a silent error in the form of Not a Number returned. Many execution errors in JavaScript are type errors. Types are one of the main foundations of a programming language. They help give a better understanding of the language and its behavior.
The types in JavaScript look simple and useless but knowing how they work is important. There are also the objects Number, Boolean, and Symbol which also add properties to its own primitives. The code "a".length is evaluated as new String("a").length by the interpreter like magic. For example, because of the String object you can retrieve the length of a string. Interestingly, the primitive types are immutable and don't have properties. JavaScript has six primitives types: string, number, undefined, null, boolean, and symbol. The type of a value also determine the operations and methods allowed to be performed on it. In a programming language, the types are what determine the amount of memory allocated to save a value. Numbers and strings are the most popular types. At the same time, we know that we can't multiply the values in the following sequence JS, is, and cool, but we can naturally merge them. Based on our Mathematical knowledge, it is easy to identify that it is possible to sum the values in the sequence 11, 3, 67, and 89. If you don't plan on changing these nested arrays or objects though, you don't need to clone them.In simple terms, a type helps group similar values on which common operations can be performed. You'd have to manually clone every layer that you plan on working with. You still have the old pointers, pointing to the old nested arrays/ objects! If you cloned array contains nested arrays or objects as elements or if your object contains properties that hold arrays or other objects, then these nested arrays and objects will not have been cloned! Here's one super-important thing to note though: You're not creating deep clones with either approach!
Now you know how to clone arrays and objects. What's actually stored in the person variable in the following snippet? var person = ) and will then pull all properties of person out of it, into the brand-new objects. The fact that only pointers are stored on the stack for reference types matters a lot! But how does that make a difference to us, the developer? #Strange Behavior of "Reference Types" This pointer in turn is stored on the stack. Obviously, there's more to it but this rough differentiation will do for now.įor each heap item, the exact address is stored in a pointer which points at the item in the heap. Since objects and arrays can be mutated and change at runtime, they have to go into the heap therefore. The heap is a memory for items of which you can't pre-determine the exact size and structure. This is the case for numbers, strings, booleans.
Only items for which the size is known in advance can go onto the stack. Here's a super-short summary: The stack is essentially an easy-to-access memory that simply manages its items as a - well - stack. JavaScript knows two types of memory: The Stack and the Heap. What's the idea behind all of that?īehind the scenes, JavaScript of course has to store the values you assign to properties or variable in memory. #What's the Difference?Ĭool, we got two different types of values. A string is a primitive value/ type as you learned but this doesn't affect the array. The hobbies array is also a reference type - in this case, it holds a list of strings. And you could of course also have nested objects or arrays inside the person object. This doesn't affect the object being a reference type though.
Please note that it holds properties that in turn have primitive values. Here, person is an object and therefore a so-called reference type. So we learned what "Primitives" (or "primitive types") are. Mastered the JavaScript basics? Take the next step with this course and dive into all new ES6 features!