DEV Community

Srividya Mysore Venkatesh
Srividya Mysore Venkatesh

Posted on

Chapter 4: Data Structures- Objects and Arrays.

This is the fourth blog in the series of twenty-one blogs that bring together my understanding from the book EloquentJS. The book at this point starts with a fictional story of a boy named Jaques, who turns into a squirrel on eating peanuts and not brushing his teeth. So to get to this result he applies all the concepts that are taught in this chapter.

Data Sets:
Say, for example, that we want to represent a collection of the numbers 2, 3, 5, 7, and 11.
We could get creative with strings and we would have to somehow extract the digits and convert them back to numbers to access them....that is a lot of work.
Fortunately, JavaScript provides a data type specifically for storing sequences of values. It is called an array and is written as a list of values between square brackets, separated by commas.
NOTE- The first index of an array is zero, not one.

Array Properties:
Expressions like myString.length and Math.max are those statements that access a property of some value.
Almost all JavaScript values have properties. The exceptions are null and undefined. If you try to access a property on one of these nonvalues, you get an error.
Two main ways to access properties in Java Script are with a Dot and Square Brackets.

Methods:
Properties that contain functions are generally called methods. Both string and array values contain, in addition to the length property, a number of properties that hold function values.

For example, toUpperCase and toLowerCase convert text to 'CAPITALS' and 'notcapitals'.

Objects:
Values of the type object are arbitrary collections of properties. One way to create an object is by using braces as an expression.
Objects are usually represented by ---> Key:value or identity:property.
braces have two meanings in JavaScript. At the start of a statement, they start a block of statements. In any other position, they describe an object.

Delete option deletes an object. But, there is a difference between setting a property to undefined and actually deleting it is that, in the first case, the object still has the property (it just doesn’t have a very interesting value), whereas in the second case the property is no longer present and in will return false.

What properties does an object have? Object.keys
Copy all properties from one object to another? Object.assign

Mutability:
Object values can be modified. The types of values, such as numbers, strings, and Booleans, are all immutable—it is impossible to change the values of those types.
Objects work differently. You can change their properties, causing a single object value to have different content at different times.

When you compare objects with JavaScript’s == operator, it compares by identity: it will produce true only if both objects are precisely the same value. Comparing different objects will return false, even if they have identical properties.

The Lycanthrope's Log:
Correlation is a measure of dependence between statistical variables. A statistical variable is not quite the same as a programming variable. In statistics you typically have a set of measurements, and each variable is measured for every measurement.
To compute the measure of correlation between two Boolean variables, we can use the phi coefficient (ϕ).
Correlation between variables is usually expressed as a value that ranges from -1 to 1.

NOTE:We can represent a two-by-two table in JavaScript with a four-element array ([76, 9, 4, 1]). We could also use other representations, such as an array containing two two-element arrays ([[76, 9], [4, 1]])

Array Loops:
This kind of loop is common in classical JavaScript—going over arrays one element at a time is something that comes up a lot, and to do that you’d run a counter over the length of the array and pick out each element in turn.

This works not only for arrays but also for strings and some other data structures.

Further Arraylogy:
Push & Pop ---> add and remove elements at end of the array
Shift & Unshift ---> add and remove elements at the start of the array
getTask() --->get front item from queue.
rememberUrgently ---> adds a task in front of the queue.
LastIndexOf ---> to search from the end instead of the start.
Slice ---> takes start and end indices and returns an array with elements in between.
conactenation ---> Refer to Blog1

Strings and Their Properties:
length ---> length of the string
slice, indexOf ---> string’s indexOf can search for a string containing more than one character, whereas the corresponding array method looks only for a single element.
Trim --->removes whitespace (spaces, newlines, tabs, and similar characters) from the start and end of a string.
Split and Join --->You can split a string on every occurrence of another string with split and join it again with join.

Rest Parameters:
It can be useful for a function to accept any number of arguments.
To write such a function, you put three dots before the function’s last parameter.
such a function is called, the rest parameter is bound to an array containing all further arguments. If there are other parameters before it, their values aren’t part of that array.

You can use a similar three-dot notation to call a function with an array of arguments.

The Math Object:
Math.max (maximum), Math.min (minimum), and Math.sqrt (square root). cos(cosine), sin(sine), tan(tangent) supports their inverse as well as acos, asin and atan. Also supports the number pi to the closest approximation.

Math.random --->This is a function that returns a new pseudorandom number between zero (inclusive) and one (exclusive) every time you call it.

There are also the functions Math.ceil (for “ceiling”, which rounds up to a whole number), Math.round (to the nearest whole number), and Math.abs, which takes the absolute value of a number, meaning it negates negative values but leaves positive ones as they are.

NOTE: Having too many global bindings “pollutes” the namespace.

JSON - JavaScript Object Notation:
If you want to save data in a file for later or send it to another computer over the network, you have to somehow convert these tangles of memory addresses to a description that can be stored or sent.
What we can do is serialize the data. That means it is converted into a flat description. A popular serialization format is called JSON.
Comments are not allowed in JSON.

Happy Reading!
SriV

Top comments (0)