Understanding Data Types in JavaScript

JavaScript employs data types to characterize the kind of data that variables can handle. These include number, string, boolean, null, undefined.

let someNumber = 5;     // a number
let someString = "Hi";  // a string
let someBool = true;    // a boolean

The type of a JavaScript variable is dynamically determined by its value, allowing the type to change in accordance with its value.

let variable = "string"; // a string
variable = 5; // now a number!
Examining 'undefined' and 'null' Data Types

JavaScript has special types: 'undefined' and 'null'. Each has only one value. 'undefined' means that a variable has been declared but does not yet have a value. 'null' implies the absence of a value. Note that we will use a special console.log method, which outputs the result in the console.

let testVar; // declared but not assigned
console.log(testVar); // Output: undefined
testVar = null; // assigned 'null'
console.log(testVar); // Output: null 

In JavaScript, 'undefined' is a type, while 'null' is an assignment value used to denote no value. You can think of 'undefined' as a bucket, into which we have forgotten to put anything, and 'null' as a bucket into which we've intentionally placed a note that says "nothing".

Exploring JavaScript Statements

Statements are instructions that JavaScript executes. Statements can declare variables, assign values, perform computations, or make decisions based on certain conditions. JavaScript executes statements in the order they appear. Also, notice how we use ; symbol at the end of each statement? In JavaScript, a semicolon (;) is like a stop sign for your code – it tells the computer where one piece of code ends and another begins. While JavaScript often understands your code without semicolons, it's good practice to use them. This helps avoid confusion and errors in your programs.

let x = 5; // Declares variable x and assigns it a value of 5
let y = 6; // Declares variable y and assigns it a value of 6
let z = x + y; // Computes the sum of x and y and assigns it to z. We can also use subtraction: x - y, multiplication: x * y, and division: x / y
console.log(z); // Outputs the value of z, which is 11

x++; // Increments the value of x by 1 using the ++ operator. x is now 6
y--; // Decrement the value of y by 1. y is now 5

x -= 3; // Decrement the value of x by 3 using -= operator. x is now 3
y += 2; // Increments the value of y by 2 using the += operator. y is now 7

x *= 4; // Multiply the value of x by 4 using *= operator. x is now 12
x /= 3; // Divide the value of x to 3 using /= operator. x is now 4

In real-world scenarios, consider statements as analogous to a cooking recipe — each step is executed in the given order, ultimately creating a dish (or, in this case, a program).

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal