Is there possibility of colouring or styling console in javascript?
Yes, we can add colours and styling for console .
Using %c:
The %c placeholder is a special syntax that enables the application of CSS styling to specified sections
of the console output. It allows programmers to format console messages according to CSS principles.
Using ANSI escape code:
The output color of the console can be modified via JavaScript using ANSI escape codes. Console
messages can have their appearance changed to improve readability and visual distinctiveness.
[Link]("\u001b[34m nikhitha");
[Link]("\u001b[33m nikhitha");
[Link]("\u001b[32m nikhitha");
[Link]("\u001b[35m nikhitha");
var str="color:blue;font-size:20px;border:2px solid black;padding:20px;"
[Link]("%c Javascript",str);
var outputcolor="color:green; font-size:30px; padding:4%;border:4px
solid;"
[Link]("%c nikhitha",outputcolor);
W hat is an array?
An array in JavaScript is a data structure used to store multiple values in a single variable. It can hold
various data types and allows for dynamic resizing.
Non-primitive and zero-indexed.
The Array object, as with arrays in other programming languages, enables storing a collection of multiple
items under a single variable name, and has members for performing common array operations.
JS arrays are resizable and can contain mix of different data types.
var a=[10,20,30];
[Link](a);
[Link](a[1]);
output:
array(3)
0: 10
1: 20
2: 30
Length:3
20
How to access elements from array?
Elements are accessed by their index, starting from 0. And can be accessed by methods also.
1. Using Square Bracket Notation
We can access elements in an array by using their index, where the index starts from 0 for the first
element. We can access using the bracket notation.
2. Using forEach Loop
In this approach, we will use a loop for accessing the element. We can use for, forEach, or for…of
methods for looping. The foreach() method allows you to iterate over all elements in the array and
perform an operation on each element.
3. Using map() Method
The Javascript map()method in JavaScript creates an array by calling a specific function on each
element present in the parent array.
4. Using find() Method
The find() method returns the first element in the array that satisfies a provided testing function.
JavaScript
5. Using Destructuring Assignment
Destructuring Assignment is a JavaScript expression that allows us to unpack values from arrays, or
properties from objects, into distinct variables data can be extracted from arrays, objects, and nested
objects and assigned to variables.
6. Using filter() Method
The filter() method in JavaScript creates a new array containing elements that pass a specified
condition. It iterates through each element of the array, executing the condition for each element and
including elements that return true in the filtered array.
Why index starts from 0?
The index of an element is how far it is from the first element.
Index is used as an offset.
For consistency- it helps to avoid confusion and errors.
Consider int arr[100]. The answer lies in the fact how the compiler interprets arr[i] ( 0<=i<100).
arr[i] is interpreted as *(arr + i). Now, arr is the address of the array or address of 0th index element
of the array. So, address of next element in the array is arr + 1 (because elements in the array are
stored in consecutive memory locations), further address of next location is arr + 2 and so on. Going
with the above arguments, arr + i mean the address at i distance away from the starting element of
the array. Therefore, going by this definition, i will be zero for the starting element of the array
because the starting element is at 0 distance away from the starting element of the array. To fit this
definition of arr[i], indexing of array starts from 0.