image
Introduction to JavaScript Variables, Primitive Data Types, and Console
Introduction to JavaScript Variables, Primitive Data Types, and Console
Difficulty
Beginner
Duration
7m
Students
1585
Ratings
4.6/5
Description

This course explores the concepts of variables and primitive data types in JavaScript and looks at how to view and manipulate these in a browser console. The course uses a hands-on demonstration to give you a real-world understanding of the topics covered in the course.

Learning Objectives

  • Learn what variables are and how to create them
  • Learn about a range of primitive data types
  • Learn how to use console log to display values using JavaScript

Intended Audience

Anyone who wants to learn about variables and primitive data types in JavaScript, or who wants to improve their knowledge of the programming language in general.

Prerequisites

This course is targeted towards absolute beginners.

 

Transcript

Introduction to JavaScript variables, primitive data types and console. So let's begin with a question. What is a variable? A variable is a named reference to a piece of data that is being stored in the system's memory. In order to create a variable, I need to start with the variable declaration statement. 

So I will begin by typing var, this tells the JavaScript interpreter that's inside of the browser, that this is a variable. Now it will give this variable the name of my name. So this variable, my name is a symbolic reference to any information that it is storing. Now, before I store any data in this variable, I'm gonna talk about JavaScript's datatypes and in JavaScript there are two types of data primitive and collection. This video will focus on primitive data types, which are data types designed to store a single value.

The first data type I will discuss as a string, and strings are pieces of data that will be represented in text form. Now, in order to give a variable a value, the data that it needs to store, the variable value needs to be assigned to it, and that assignment operator is the equal sign. So now to the right of my name, I'm gonna type equal. Now I will type the value that I'm gonna assign to this variable. So using my name as an example, I will type Farish wrapped in quotes, in JavaScript anytime a string is declared that value needs to be wrapped in quotes. So the interpreter knows that it is a string. 

Now, what if I wanted to display the value of the my name variable? Due to the nature of this environment, we do have the browser console to the right, and this gives us access to the built-in function console.log that I will demonstrate. So on the line below, I will type consult.log with parentheses and inside of the parentheses, I have to pass through our argument, which is in this case, my name, the variable. So I will type in my name, now I will hit run and on the right, we can see my name being displayed in the console.

Now console log is primarily used as a messaging system for debugging, but in this environment, we're gonna use it to display values. Now, hypothetically, what if later on in the program you wanna change the value of the my name variable? Well, that is called variable reassignment, and to demonstrate this on the line below, I will type my name and then equal sign, and now I will give it a value of Lori, which is a string, so I'll wrap it in quotes.

Note that I did not use the keyword var when reassigning the value of the my name variable. If I were to use var, I would be re-declaring the variable, creating a new instance of this variable overriding the previous one in the memory. Now I'm gonna hit run again to display the current value of my name in the console and to the right we can see it's now Lori, the next primitive data type I will discuss is a number which is used for numeric data.

Now, while other languages have additional number types, such as floats, integers, doubles and bignums, JavaScript is not, all those earlier formats that I previously mentioned are supported by the number of data type, up to double precision 64 bit floating point format.

So on the next line below, I'm gonna declare a number, so I'm gonna make a variable called var number equal 20 semi-colon. If I were to wrap the number in quotes, it would be treated as a string, not as a number.

Now the next primitive I will discuss is boolean, which is a binary data type with only two possible values, true or false. In on the next line, I'm gonna write out an example, var invoice paid equals true. I'm gonna discuss a hypothetical situation where a variable like this may be used, invoicing software.

Typically in most invoicing software, there's a status of whether an invoice has been paid or not. When the invoice is generated, the default paid status is usually set to false, and then when it's paid that status is changed from false to true, and this is one of many potential scenarios where a boolean value would be used.

Now I'm gonna clean up this environment a little bit and go back to the var, my name variable, and on the screen we see var my name, but it hasn't been assigned any value. And now am gonna console log my name and hit run. And to the right, the value of undefined is in the console, which is in another primitive data type in JavaScript, which only has one possible value undefined. And by default, when the variable's declared, but never assigned a value, it will always be assigned a value of undefined. And you may be wondering in what scenario would you use a undefined variable? Well, you might be declaring the variable, but you don't wanna set it within initial value later on in the program, you will assign a value to that variable. This allows you to create a global reference to a variable that is stored in the system's memory for later use. And that's it for JavaScript variables, primitive data types and console log. Thanks for watching at Cloud Academy.

About the Author
Students
7097
Labs
24
Courses
73
Learning Paths
31

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.

Covered Topics