JavaScript Values

JavaScript values can be easier to understand when we relate them to everyday scenarios. Let’s explore some of the basic values in JavaScript using simple, real-life examples:

1. NaN (Not a Number)

Imagine you’re trying to calculate the average age of a group, but one person mistakenly enters “hello” instead of their age. This error leads to a result that isn’t a valid number:

const participantAge = 'hello';
const averageAge = participantAge / 5;
console.log(averageAge); // Output: NaN

In this case, averageAge is NaN because you can’t do math with text.

2. 0 (Zero)

Think of a newly added shelf that has no books on it:

const booksOnShelf = 0;
console.log(booksOnShelf); // Output: 0

Here, booksOnShelf shows there are no books on the shelf.

3. Undefined

Imagine looking for an item in a box, but it’s missing:

let missingItem;
console.log(missingItem); // Output: undefined

missingItem is undefined because it wasn’t given a value, similar to finding an empty box.

4. True

Suppose you’re checking if a light bulb is turned on:

const isLightOn = true;
console.log(isLightOn); // Output: true

isLightOn confirms that the light bulb is on.

5. Strings

Think of writing a list of ingredients for a recipe:

const recipeIngredients = 'flour, sugar, eggs, milk';
console.log(recipeIngredients); // Output: 'flour, sugar, eggs, milk'

recipeIngredients is a string listing what you need to bake something.

6. Objects

Imagine organizing your kitchen utensils:

const kitchenUtensils = { knives: 5, spoons: 10, forks: 8 };
console.log(kitchenUtensils); // Output: { knives: 5, spoons: 10, forks: 8 }

kitchenUtensils holds details about how many of each item you have.

7. Arrays

Picture sorting fruits in a basket:

const fruitBasket = ['apple', 'banana', 'orange', 'grape'];
console.log(fruitBasket[0]); // Output: 'apple'
console.log(fruitBasket.length); // Output: 4

fruitBasket is an array with fruits. You can find specific fruits by their position or get the total count.

8. Functions

Think of following a recipe to bake a cake:

function bakeCake() {
console.log('Mix ingredients, bake in oven, and enjoy!');
}
bakeCake(); // Output: 'Mix ingredients, bake in oven, and enjoy!'

bakeCake is a function that lists the steps for baking, and when you use it, it performs those steps.

 

JavaScript ValueCode ExampleReal-life AnalogyDescription
NaNconst averageAge = ‘hello’ / 5;Trying to divide “hello” by a numberOccurs when performing invalid math operations with non-numeric values.
0const booksOnShelf = 0;An empty bookshelfRepresents a count of zero, indicating absence.
Undefinedlet missingItem;Searching for a missing item in an empty boxA variable declared but not assigned a value, similar to looking for something that isn’t there.
TRUEconst isLightOn = true;A light bulb that is turned onRepresents a positive condition, meaning something is true or “on.”
Stringsconst recipeIngredients = ‘flour, sugar, eggs, milk’;Writing down a recipe listStores text or sequences of characters, like making a note of ingredients.
Objectsconst kitchenUtensils = { knives: 5, spoons: 10 };Organizing different kitchen utensilsGroups related data into a single entity, like counting utensils by type.
Arraysconst fruitBasket = [‘apple’, ‘banana’];A basket containing various fruitsAn ordered list of items, which can be accessed by their position (index).
Functionsfunction bakeCake() { … }Following a recipe to bake a cakeDescribes a set of steps or instructions that can be reused and executed.
Scroll to Top