variables<\/span> display their associated value in the console.<\/p>\n\n\n\nCoercion and Mutation<\/h2>\n\n\n\n So far you have learned about variables<\/span> and the types of data<\/span> that can be stored within them. The data<\/span> types (string, number, boolean, etc.) are tied to the value<\/span> in JavaScript<\/span>, and not the variable<\/span> container<\/span> itself<\/strong>. For example, if I have a variable<\/span> called gpa<\/strong> and store 3.6 (number) as the value<\/span>, I can later coerce this into a string by combining (or concatenating) it with other variables<\/span>. If I combine studentName and gpa by using the + function in JavaScript<\/span>, the resulting output will be provided in a string.<\/p>\n\n\n\nOther languages, such as Java and C# enforce strict typing, meaning the variable itself (in this case student) would be assigned a type of string. If I later tried to store the boolean true value, I would receive a compilation error.<\/p><\/blockquote>\n\n\n\n
Similarly, because JavaScript<\/span> does not enforce the data<\/span> type at the variable<\/span>, different values of different data<\/span> types can be stored<\/span> over time. Just like coercion, which happens when variables<\/span> are coerced into a different type, mutation can occur when a different value<\/span> of a different type is stored<\/span> in the variable<\/span>. Take the gpa variable<\/span> from the example above. In the code it is a value<\/span> set to 3.6 (number). We can however update this variable<\/span> and store<\/span> a value<\/span> of “three point six” (string). Both coercion and mutation are made possible by using JavaScript<\/span>‘s dynamic typing. To summarize, JavaScript<\/span> will allow you to store<\/span> values of any type within a variable<\/span> without enforcement. Let’s look at a couple of examples to better understand this process.<\/p>\n\n\n\nIn your script.js file<\/span>, update the code, adding the new lines on the end starting with the comments<\/span> ‘Coercion and Mutation’:<\/strong><\/p>\n\n\n\n\/**\n* Variables and Types\n*\/\n\nlet studentName = 'Fred Jones'; \/\/ string. Strings are always stored in quotes \nconsole.log(studentName)\n\nlet gpa = 3.6; \/\/ number. Numbers are stored without quotes \nconsole.log(gpa)\n\nlet graduated = false; \/\/ boolean. Either true or false can be assigned \nconsole.log(graduated)\n\nlet quizResult; \/\/ undefined\nconsole.log(quizResult)\n\nlet emptyValue = null; \/\/ null\nconsole.log(emptyValue)\n\n\/**\n* Coercion and Mutation\n*\/\n\n\/\/ Type Coercion Example\nconsole.log(studentName + ' ' + gpa) \/\/ The + concatenates studentName with an empty space and gpa is coerced into a string\n \n\/\/ Variable Mutation Example\ngpa = \"three point six\"; \/\/ JavaScript changes the data type to string and updates the gpa variable\nconsole.log(gpa)<\/code><\/pre>\n\n\n\nYou should now feel comfortable with the JavaScript<\/span> fundamental programming concept of data<\/span> types, variables<\/span>, and dynamic typing.<\/p>\n","protected":false},"excerpt":{"rendered":"Introduction to JavaScript with an overview of data types, values and variables.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[11499,8212,7349],"post_series":[7348],"class_list":["post-7733","post","type-post","status-publish","format-standard","hentry","category-coding","tag-bitesize-javascript","tag-cmst-388","tag-javascript","post_series-bitesize-javascript"],"yoast_head":"\n
JavaScript Data Types, Values, and Variables - Ian Carnaghan<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n