Data Types in Java Script:
JavaScript, as a dynamically-typed language, provides several data types that help developers define the nature of variables and manipulate data within their programs. These data types include:1. Primitive Data Types:
Number: Represents both integer and floating-point numbers.
String: Represents a sequence of characters, enclosed in single (' ') or double (" ") quotes.
Boolean: Represents a logical value of either true or false.
Null: Represents the intentional absence of any object value.
Undefined: Represents a variable that has been declared but not assigned any value.
Symbol: Represents unique and immutable values and is often used as object property keys.
Example :
let a = 10;
let b = 15.50;
console.log(a+b);
let str = "chandan";
let str2 = 'singh';
console.log(str + " " +str2);
let str3 = `str ${a*10}`;
console.log(str3);
let bo = true;
if(bo){
console.log("its true")
}
2. Composite Data Types/Non-primitive data type:
Object: Represents a collection of key-value pairs where keys are strings (or Symbols) and values can be of any data type, including functions, other objects, etc.
Array: A special type of object used to store a collection of elements. Arrays can contain any data type, and elements are accessed by their numeric indices. JavaScript also supports special data structures and object types like Map, Set, Date, RegExp, etc., each with its own unique characteristics and functionalities.The dynamic nature of JavaScript allows variables to adapt to different data types as needed during runtime, which gives developers flexibility but also requires careful consideration to avoid unexpected behaviors due to type coercion or implicit type conversions.
ex : let arrayName = [element1, element2, element3, ...];
Objects in Java Script
In JavaScript, objects are a fundamental data type that allows for the creation of collections of key-value pairs. Objects are used to store data in the form of properties and methods. They are versatile and can represent complex entities by grouping related data and functionalities together.Objects in JavaScript are defined using curly braces {} and contain zero or more key-value pairs. Keys (also known as property names) are strings or Symbols, and values can be of any data type, including other objects, arrays, functions, etc.
Here's an example of creating an object in JavaScript:
Array in Java Script
In JavaScript, an array is a collection of elements, which can include numbers, strings, objects, other arrays, and more. Arrays are particularly useful for storing lists of items and are zero-indexed, meaning the first element has an index of 0.
Key Features of Arrays in JavaScript
Flexible Data Types: Arrays can hold any data type, including strings, numbers, objects, and even other arrays (making it possible to create multidimensional arrays).
Example :
let mixedArray = [42, "hello", true, { name: "Alice" }, [1, 2, 3]];
Dynamic Length: The length of an array can change dynamically. You can add or remove elements as needed.
Accessing Elements: Elements are accessed by their index, starting from 0
Example :
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]); // Output: "banana"
Array Properties and Methods:
length: Returns the number of elements in the array.
Example :
console.log(fruits.length); // Output: 3
push(element): Adds one or more elements to the end of the array.
Example :
fruits.push("orange"); // ["apple", "banana", "cherry", "orange"]
pop(): Removes the last element from the array and returns it
Example :
let lastFruit = fruits.pop(); // "orange"
unshift(element): Adds one or more elements to the beginning of the array
Example :
fruits.unshift("kiwi"); // ["kiwi", "apple", "banana", "cherry"]
shift(): Removes the first element from the array and returns it.
Example :
let firstFruit = fruits.shift(); // "kiwi"
splice(start, deleteCount, ...items): Adds/removes elements from a specific position in the array.
Example :
fruits.splice(1, 1, "grape"); // Replaces "apple" with "grape"
slice(start, end): Returns a shallow copy of a portion of an array
Example :
let newFruits = fruits.slice(1, 3); // ["banana", "cherry"]
indexOf(element): Returns the index of the first occurrence of the specified element, or -1 if it does not exist.
Example :
console.log(fruits.indexOf("cherry")); // Output: 2