Objects are complex data type
Const myObj = {};
console.log(myObj);
const person = {
firstName : ‘S’,
lastName: ‘Man’,
Age: 4
}
person.lastName = ‘Johnson’;
person.numberOfGuitars = 3;
console.log(person);
Objects destructuring : assigning properties of an object to individual variables
const {firstName, age} = person;
console.log(“ghost emoji”, firstName, age);
Consider this example
const person = {
‘First Name’ : ‘S’,
lastName: ‘Man’,
Age: 40
}
To get the ‘First Name’ we have to do this:
person.[‘First Name’]
Also object keys should be strings, the values can be anything
Q) why does the index start with zero in javascript?
A: This one’s rooted in the history of computer science. Zero-based indexing traces
back to the C programming language, created in the early '70s. Arrays in C are
designed to start at the beginning of a memory block, and indexing from zero
simplifies the computation of an element’s address. JavaScript inherits many conventions
from C, including zero-based indexing. Once this became standard, many languages
that came afterward stuck with it to keep things consistent and predictable.
It's a nod to the past that still makes sense in today's coding world.
array.pop() - removes the last value and
array.push() - adds a value at the end of an array
array.concat() - takes 2 arrays and merge it to 1 array
array.slice()
Destructuring an array
const arr = [‘apple’,’orange’,’banana’];
const [first,second,third] = arr;
console.log(first); // apple
Array can be multi dimensional, can be array of arrays
const arr=[[1,2,3], [4,5,6], [7,8,9]];
console.log(arr[1][2]); // 6
const arr=[[1,2,3], [4,[‘a’,’b’,’v’],6], [7,8,9]];
Reference vs primitive data types :
Reference types are like class and arrays, when copies are made reference are copied
Primitives are like string, numbers, booleans, when copies are made values are copied
const name = ‘james’;
const firstName = name;
console.log(firstName);
Easy way to know is if we make a copy and cannot change the value then it is primitives
type when we precede with ‘const’
Eg of reference type variables
const arr=[1,2,3]
const narr = arr
arr[1] = 5
console.log(arr) // [1,5,3]
console.log(narr) // [1,5,3]
Spread operators (...) are incredibly handy in JavaScript! They allow you to easily expand elements of an array or object.
When it comes to arrays, you can use them to create a new array that includes elements from an existing array. For example:
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
const arr3 = [...arr2] // copy by value
const arr4 = arr3 // copy by reference
console.log(arr2); // Output: [1, 2, 3, 4, 5, 6]
const arr2 = [ 4, 5, 6,...arr1];
const arr2 = [ 4, 5,...arr1,6];
You can also use them for copying arrays, combining arrays, and even spreading elements as function arguments.
object copy by value, using spread operator
const billRodgersIV={...willRodgersIII}
const russell = {
touchdowns:2,
yards:1123,
sacks:4
}
copy other values as is but changed the touchdowns value to 5
const russellWeek17 = {
...russell,
touchdowns:5,
interceptions : 1234 // and adding new properties like this
}
RegEx : way of validation of input from user
Quiz notes :
In JavaScript, null and undefined don't have properties. Here's why:
Null: Represents the intentional absence of any object value. It's like a placeholder to say"there's no value here." Since it signifies the absence of an object, it naturally doesn't have
properties or methods.
Undefined: Indicates a variable that hasn't been assigned a value yet.
It's the default value for uninitialized variables, and since it means "no value yet,"
it doesn't have properties either.
On the other hand, numbers, strings, and arrays do have properties and methods. Numbers and strings have
built-in methods (like .toFixed() for numbers or .length for strings), and arrays are objects in JavaScript,
so they come packed with a host of properties and methods.
dot notation and bracket notation are the right ways to access properties in JavaScript objects.
Dot notation is simple and straightforward: object.property
Bracket notation allows for dynamic property access, where the property name is specified as aArrow notation isn’t a thing when accessing object properties. It’s actually used for functions and doesn’t
relate to property access.
No comments:
Post a Comment