Logo
Unit 2 – JavaScript Variables and Data Types

JavaScript Variables and Data Types

Duration: 5 minutes

Hello, future web designers!

Now that we’ve greeted JavaScript for the first time, we’re prepared to delve further. You will learn about variables and data types in this session, two essential components of JavaScript programming.

Understanding Variables and Data Types

In JavaScript, a variable is simply just designated storage for information that you might wish to access or change in the future. It’s critical to follow a few rules when naming variables:

  • The name of a variable must be preceded by a letter, an underscore (_), or a dollar symbol ($). They are unable, to begin with a number.
  • They may also contain underscores (_), dollar signs ($), and alphanumeric letters (A-Z, a-z, 0-9).
  • Variable names are case-sensitive.
  • JavaScript has reserved words that you should avoid using as variable names to prevent conflicts. Examples are “function”, “return”, “var”, and many others.

JavaScript supports several data types, but for this tutorial, we’ll focus on the basics:

Data TypesDescriptionExample
StringSequences of characters wrapped within single (’ ’) or double (” ”) quotes. It can hold text, numbers, and symbols.”Hello, JavaScript!”, ‘abc123’, ‘JS is versatile!’
NumberNumeric data types, including integers and floating-point numbers.1, 2.5, -15, 100.23
BooleanData type with only two possible values: true or false, mainly used in logical operations.true, false
UndefinedA variable that has been declared but has not yet been assigned a value.let example;
NullRepresents a deliberate absence of any value. It’s different from ‘Undefined’.let emptyValue = null;

Writing Your First Variables

Let’s create some JavaScript code to showcase these variable types:

// a number
let num = 5;
console.log(num);
// a string
let greeting = "Hello, JavaScript!";
console.log(greeting);
// a boolean
let isJsFun = true;
console.log(isJsFun);
// an undefined variable
let undefinedVar;
console.log(undefinedVar);
// a null value
let nullValue = null;
console.log(nullValue);

Here, ‘num’, ‘greeting’, ‘isJsFun’, ‘undefinedVar’, and ‘nullValue’ are variable names, and ‘5’, “Hello, JavaScript!”, ‘true’, ‘undefined’, and ‘null’ are their respective values.

The console.log function is used to display the value of each variable.

Exercise

Now is your chance. Declare a number, a string, a boolean, and a variable with a null value. Give them names of your choosing, give them appropriate values, and use console.log to see their contents. You’ll deepen your understanding of JavaScript variables and data types through this practical job.

Conclusion

Congratulations on finishing this module! You now possess the fundamental understanding of declaring and working with variables in JavaScript. We’ll be delving into even more complex topics as we go along, expanding on what you’ve studied so far.

Onward to more coding adventures!

Next Tutorial: JavaScript Operators

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!