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.

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Build modern and responsive webpages

The Modern Web

Build modern and responsive webpages Try Front-end

The Modern Web will guide you through the most important tools of device-agnostic web development, including HTML5, CSS3, and JavaScript. The plain-english explanations and practical examples emphasize the techniques, principles, and practices that you'll need to easily transcend individual browser quirks and stay relevant as these technologies are updated.

$ Check price
34.9534.95Amazon 4.5 logo(30+ reviews)

More Front-end resources

Ad