03. JavaScript Values literals and variables

Literals

Literals are fixed values of either numbers (with or without decimals), or strings.

// numerical literals
3.14;
48;
 
// Strings
"Hello World!";
'Hello Mars!'; 

Literals won't do much on its own, so let's take a look at variables, which are containers that can store literals.

Variables

Variables store data values such as literals. They can also be set to expressions, which are evaluated and placed into the variable.

To define a variable, we use the var keyword with the assignment operator = symbol.

// Initialize variable count and set it to 0
var count = 0;
// Or just initialize the variable 
var count;

In the second initialization, the variable count is initalized to undefined.

Declaring Multiple variables

You can also declare multiple variables while using the var keyword just once.

var price = 5, quantity = 23, total = 50;
var price, quantity, total;

A loosely typed language

JavaScript is not a strongly typed language, meaning you don't need to declare what type of data a variable will hold. So no matter what data type we're declaring, we use the var keyword. We'll talk about the different data types and typecasting in the next section.

Guidelines for variable

Here are some guidelines to follow when assigning a variable name:

  1. Variable names cannot start with a number.
  2. Valid characters only include letters, numbers, $ and _.
  3. Variables are case sensitive!
  4. There are some keywords in JavaScript that you cannot use.

Variable naming

When deciding on a variable name, it's important to be clear and descriptive. Names like p and pre are too short and don't convey any information.

Variables of more than one word should have a upperCamelCase format. This means that the first word is all lowercase, and any subsequent words have their first letters capitalized. For example, a variable that holds the number of remaining books would look like numRemainingBooks.

Build modern and responsive webpages

HTML & CSS

Build modern and responsive webpages Try Front-end

This book teaches HTML as if it's for anyone - hobbyists, students, and professionals - and it's full-color throughout. It utilizes information graphics and lifestyle photography to explain the topics in a simple way that is engaging. You can progress through the chapters from beginning to end or just dip into topics of particular interest at your leisure.

$ Check price
29.9929.99Amazon 4.5 logo(926+ reviews)

More Front-end resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad