diff --git a/README.md b/README.md index 4b7ec11..7531509 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,7 @@ Author:Ayush Shrivastav\ Copyright 2020 \ 1- Introduction to JavaScript.\ 2- Conditionals. \ -3- Functions in Js +3- Functions in Js. \ +4- Scope in js.\ +5- Arrays in Js.\ +6- Loops in Js. diff --git a/arrays.js b/arrays.js new file mode 100644 index 0000000..505ab26 --- /dev/null +++ b/arrays.js @@ -0,0 +1,94 @@ + Arrays +-- Arrays are lists of ordered, stored data. They can hold items that are of any data type. +Arrays are created by using square brackets, with individual elements separated by commas. + +// An array containing numbers +const numberArray = [0, 1, 2, 3]; + +// An array containing different data types +const mixedArray = [1, 'chicken', false]; + + +-- Index +Array elements are arranged by index values, starting at 0 as the first element index. Elements can be accessed by their index using the array name, and the index surrounded by square brackets. + +// Accessing an array element +const myArray = [100, 200, 300]; + +console.log(myArray[0]); // 100 +console.log(myArray[1]); // 200 +console.log(myArray[2]); // 300 + + +-- Mutable +JavaScript arrays are mutable, meaning that the values they contain can be changed. + +Even if they are declared using const, the contents can be manipulated by reassigning internal values or using methods like .push() and .pop(). + +const names = ['Alice', 'Bob']; + +names.push('Carl'); +// ['Alice', 'Bob', 'Carl'] + + +-- Property .length +The .length property of a JavaScript array indicates the number of elements the array contains. + +const numbers = [1, 2, 3, 4]; + +numbers.length // 4 + + +--Method .push() +The .push() method of JavaScript arrays can be used to add one or more elements to the end of an array. .push() mutates the original array returns the new length of the array. + +// Adding a single element: +const cart = ['apple', 'orange']; +cart.push('pear'); + +// Adding multiple elements: +const numbers = [1, 2]; +numbers.push(3, 4, 5); + + +-- Method .pop() +The .pop() method removes the last element from an array and returns that element. + +const ingredients = ['eggs', 'flour', 'chocolate']; + +const poppedIngredient = ingredients.pop(); // 'chocolate' +console.log(ingredients); // ['eggs', 'flour'] + + +-- More Array methods +Some arrays methods that are available to JavaScript developers include: .join(), .slice(), .splice(), .shift(), .unshift(), + and .concat() amongst many others. Using these built-in methods make it easier to do some common tasks when working with arrays. + +Below, we will explore some methods that we have not learned yet. +You should also consult the MDN documentation to learn what each method does ! + +-- Use the .shift() method to remove the first item from the array + +-- Use the .unshift() method to addd an item at the beginning of the array + +-- slice() +The slice() method returns the selected elements in an array, as a new array object. + +The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. + +Imp: The original array will not be changed i.e it does not mutate the array. + + +-- splice() +The splice() method adds/removes items to/from an array, and returns the removed item(s). +Note: This method changes the original array i.e it makes the array mutable. + +Syntax- +array.splice(index, howmany, item1, ....., itemX) + Parameter Values +Parameter- +index Required. An integer that specifies at what position to add/remove items, use negative values to specify the position from the end of the array. + +howmany Optional. The number of items to be removed. If set to 0, no items will be removed. + +item1, ..., itemX Optional. The new item(s) to be added to the array. \ No newline at end of file diff --git a/loops.js b/loops.js new file mode 100644 index 0000000..1d10cac --- /dev/null +++ b/loops.js @@ -0,0 +1 @@ + Loops in JS diff --git a/scope.js b/scope.js new file mode 100644 index 0000000..6d99a5e --- /dev/null +++ b/scope.js @@ -0,0 +1,55 @@ + Scope of Variable + +Scope is a concept that refers to where values and functions can be accessed. + +Various scopes include: + +-Global scope (a value/function in the global scope can be used anywhere in the entire program) + +-File or module scope (the value/function can only be accessed from within the file) + +-Function scope (only visible within the function), + +-Code block scope (only visible within a { ... } codeblock) + +- // example +function myFunction() { + + var pizzaName = "Volvo"; + // Code here can use pizzaName + +} + +// Code here can't use pizzaName + + +-- Global Variables +JavaScript variables that are declared outside of blocks or functions can exist in the global scope, which means they are accessible throughout a program. Variables declared outside of smaller block or function scopes are accessible inside those smaller scopes. + +Note: It is best practice to keep global variables to a minimum. + +// Variable declared globally +const color = 'blue'; + +function printColor() { + console.log(color); +} + +printColor(); // Prints: blue + + +-- Block Scoped Variables +const and let are block scoped variables, meaning they are only accessible in their block or nested blocks. In the given code block, trying to print the statusMessage using the console.log() method will result in a ReferenceError. It is accessible only inside that if block. + +const isLoggedIn = true; + +if (isLoggedIn == true) { + const statusMessage = 'User is logged in.'; +} + +console.log(statusMessage); + +// Uncaught ReferenceError: statusMessage is not defined + +--Note: We should not use global scope unless required as it is a bad practice. + Scope should be limited till the variable is used. Also it makes the code looks clean and increases readability for other Developers. \ No newline at end of file