JavaScript values refer to the different types of data that can be stored and manipulated in a JavaScript program. These values include primitive data types such as numbers, strings, booleans, null, undefined, symbols, as well as object types such as arrays, functions, and objects. Let's delve into each of these value types with examples.



1. Numbers:

Numbers in JavaScript are represented using the number data type. They can be integers or floating-point numbers (decimals).


Example:
 

let num1 = 42; // Integer
let num2 = 3.14; // Floating-point number



2. Strings:

Strings are sequences of characters enclosed in single ('') or double ("") quotes.


Example:
 

let str1 = 'Hello'; // Single quotes
let str2 = "World"; // Double quotes



3. Booleans:

Booleans represent true or false values and are often used in conditional expressions.


Example:
 


let isTrue = true;
let isFalse = false;
 



4. Null and Undefined:

null represents the intentional absence of any value, while undefined signifies a variable that has been declared but not assigned a value.


Example:
 


let emptyValue = null;
let undefinedValue;
console.log(undefinedValue); // undefined
 



5. Symbols:

Symbols are unique and immutable values that are often used as property keys in objects to prevent name clashes.



Example:
 


const sym1 = Symbol('description');
const sym2 = Symbol('description'); // Different from sym1
 



6. Arrays:

Arrays are ordered lists of values that can contain any data type, including other arrays.


Example:
 


let numbers = [1, 2, 3, 4, 5];
let mixedArray = [1, 'hello', true, [6, 7, 8]];
 



7. Objects:

Objects are collections of key-value pairs where keys are strings (or Symbols) and values can be any data type, including functions and other objects.



Example:

 


let person = {
    name: 'Alice',
    age: 30,
    hobbies: ['reading', 'painting'],
    greet: function() {
        console.log('Hello!');
    }
};
 




8. Functions:

Functions in JavaScript are first-class citizens and can be assigned to variables, passed as arguments, and returned from other functions.


Example:
 


function add(a, b) {
    return a + b;
}
let result = add(5, 10); // 15
 



9. Regular Expressions:

Regular expressions (regex) are used for pattern matching within strings and are represented by a pattern enclosed in slashes (/).


Example:
 


let pattern = /[A-Z]/; // Matches uppercase letters
let emailPattern = /\S+@\S+\.\S+/; // Matches email addresses
 




10. Dates:

Dates in JavaScript are represented using the Date object, which allows manipulation and formatting of dates and times.


Example:

 


let currentDate = new Date();
console.log(currentDate); // Current date and time
 



11. Promises:

Promises represent the eventual completion or failure of an asynchronous operation and are widely used for handling asynchronous tasks.



Example:
 

let promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('Data fetched successfully');
    }, 2000);
});
promise.then((data) => {
    console.log(data);
});



12. Nullish Coalescing Operator (??):

The nullish coalescing operator ?? is used to provide a default value when dealing with null or undefined values.



Example:
 

let username = null;
let defaultName = username ?? 'Guest';
console.log(defaultName); // Guest



13. BigInt:

The BigInt type is used to represent integers with arbitrary precision, allowing computations on large numbers beyond the normal number type limits.


Example:
 


const bigNumber = BigInt(9007199254740991);
console.log(bigNumber); // 9007199254740991n
 



14. Maps and Sets:

Maps and Sets are data structures introduced in ES6 for storing key-value pairs and unique values, respectively.



Example:
 

let myMap = new Map();
myMap.set('key1', 'value1');
let mySet = new Set([1, 2, 3, 3, 4, 5]);
console.log(mySet); // Set { 1, 2, 3, 4, 5 }


15. JSON (JavaScript Object Notation):

JSON is a lightweight data interchange format often used for sending data between a server and a web application.


Example:

 


let jsonData = '{"name": "John", "age": 30}';
let parsedData = JSON.parse(jsonData);
console.log(parsedData.name); // John
 



16. Template Literals:

Template literals allow embedding expressions and multi-line strings in JavaScript.


Example:
 

let name = 'Alice';
let greeting = `Hello, ${name}!
How are you?`;
console.log(greeting);



17. Iterators and Generators:

Iterators are objects that define a sequence and can be looped over, while generators are functions that enable pausing and resuming execution.


Example:

 

function* countGenerator() {
    let count = 0;
    while (true) {
        yield count++;
    }
}
let counter = countGenerator();
console.log(counter.next().value); // 0



18. ArrayBuffer and Typed Arrays:

ArrayBuffer and Typed Arrays provide low-level memory manipulation and efficient handling of binary data.


Example:
 

let buffer = new ArrayBuffer(8);
let intArray = new Int32Array(buffer);
intArray[0] = 42;
console.log(intArray); // Int32Array [ 42, 0, 0, 0 ]



19. Proxy Objects:

Proxy objects allow custom behavior to be defined for fundamental operations on objects.


Example:

 

let targetObj = {};
let handler = {
    get: function(target, prop, receiver) {
        return prop in target ? target[prop] : 'Property not found';
    }
};
let proxyObj = new Proxy(targetObj, handler);
console.log(proxyObj.name); // Property not found



20. Symbols in Objects:

Symbols can be used as keys in objects to create private properties or define unique behavior.


Example:
 


const privateProperty = Symbol('private');
let obj = {
    [privateProperty]: 'This is a private property'
};
console.log(obj[privateProperty]); // This is a private property
 


 



Practice Excercise Practice now