Variables and Data Types
let, const, and JavaScript's main value types.
JavaScript has two everyday keywords for creating variables:
constfor values that will not be reassigned. Use this by default.letfor values that will change later.
You may see var in older code; it still works but has confusing scoping rules, so modern code avoids it.
JavaScript
const course = "JavaScript Basics";
let lessonsDone = 1;
lessonsDone = lessonsDone + 1;
console.log(course, "-", lessonsDone, "lessons done");
Output
JavaScript Basics - 2 lessons done
Types of values
The typeof operator tells you the type of a value.
JavaScript
console.log(typeof "text");
console.log(typeof 42, typeof 3.5);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof { name: "Asha" });
console.log(typeof [1, 2, 3]);
Output
string number number boolean undefined object object
- string: text, in single quotes, double quotes or backticks.
- number: whole numbers and decimals are the same type.
- boolean:
trueorfalse. - undefined: a variable that has not been given a value yet.
- object: groups of named values, and also arrays.
Template literals
Strings written with backticks can contain ${...} placeholders. Any expression inside is evaluated and inserted.
JavaScript
const item = "notebook";
const price = 45;
const qty = 3;
console.log(`${qty} x ${item} = Rs ${price * qty}`);
Output
3 x notebook = Rs 135
Arrays and objects
An array is an ordered list. An object stores values under names called keys.
JavaScript
const scores = [72, 88, 95];
const student = { name: "Asha", year: 2, scores };
console.log(scores.length, scores[0]);
console.log(student.name);
console.log(student);
Output
3 72
Asha
{ name: 'Asha', year: 2, scores: [ 72, 88, 95 ] }