Results: 62
In HTML5, the Geolocation API is used to obtain the user's geographical location. Since this can compromise user privacy, the option is not available unless the user approves it. Note:
Geolocation is much more accurate for devices with GPS, like smartphones
Storing a Value
localStorage.setItem("key1", "value1");
Getting a Value
//this will print the value
alert(localStorage.getItem("key1"));
Removing a Value
localStorage.removeItem("key1");
Removing All Values
localStorage.clear();
Note:
The same syntax applies to the session storage, with one difference: Instead of localStorage, sessionStorage is used
call JavaScript function using href attribute of <a>
<a href="javascript:get_next_10();">some link</a>
function "call" CODE
If we call
driveCar()
function using
call()
function,
this
will refer to the object that will be passed. In this case:
john
object
let john = {
  firstName: 'John',
  lastName: 'Doe'
}

function driveCar() {
  document.write(this.firstName + ' ' + this.lastName + ' is driving a car<br/>')
}

driveCar.call(john)
filter CODE
The function
filter
filters the array members based on the
getNames
function condition
let myFavoritePlanets = [
    {name: 'Earth', radius: 6371}, 
    {name: 'Jupiter', radius: 69911}, 
    {name: 'Neptune', radius: 24622, moons: ['Triton', 'Thalassa', 'Nereid', 'Proteus', 'Hippocamp', 'Naiad']}
]
let biggerPlanetsNames = myFavoritePlanets.filter(getNames);

function getNames(planet) {
  return planet.radius > 20000
}
console.log(biggerPlanetsNames );
splice CODE
Function
splice
changes content of an array. Let's first create an array of integers
let myFavoriteNumbers = [5, 14, 16, 18, 23, 25, 27, 50, 70]
If we want to remove one element from an array, we need to pass
1
as the second argument and the index of the item as the first argument. In this example
14
(second item) will be removed by passing
1 and 1
as arguments
myFavoriteNumbers.splice(1, 1);  // [5, 16,18, 23, 25, 27, 50, 70]
To remove two adjacent items, we need to pass first item's index
2
and quantity of items
2
that we want to remove. In this example we want to remove
18
and
23
from the array at the same time
myFavoriteNumbers.splice(2, 2);  // [5, 16, 25, 27, 50, 70]
Replaces 3 items starting from index 2 with
27
,
29
and
31
myFavoriteNumbers.splice(2, 2, 27, 29, 31);  // [5, 16, 27, 29, 31, 50, 70]
Removes all the items of the array except the first and the last items
myFavoriteNumbers.splice(1, 5);  // [5,70]
Adds the two new items
100
and
200
to the array after the first item
myFavoriteNumbers.splice(1, 0, 100, 200);  // [5, 100, 200, 70]
Add new item to an array CODE
Creates
myFavoriteNumbers
array
let myFavoriteNumbers = [5, 14, 16, 18]
Adds new item
22
to the array
myFavoriteNumbers.push(22)
Adds the array as a new item to the
myFavoriteNumbers
array
myFavoriteNumbers.push([23, 24])
Adds the object to the array as new item
myFavoriteNumbers.push({firstNumber: 25, secondNumber: 26})
Adds the function to the array as new item
myFavoriteNumbers.push(function(){console.log('I am fired!')})
Adds new string item to the array
myFavoriteNumbers.push('27')
Let's fire the function that we added as an item
myFavoriteNumbers[7]()
After running the function the result will be:
I am fired!
Different types of arrays CODE
Creates array of numbers;
let myFavoriteNumbers = [5, 14, 16, 27]
Creates array of strings
let myFavoriteColors = ['green', 'yellow', 'red']
Creates array of arrays
let myMatrix = [
    [12, 23, 34], 
    [13, 35, 57], 
    [14, 47, 70], 
]
Creates array of objects
let myFavoritePlanets = [
    {name: 'Earth', radius: 6371}, 
    {name: 'Jupiter', radius: 69911}, 
    {name: 'Neptune', radius: 24622, moons: ['Triton', 'Thalassa', 'Nereid', 'Proteus', 'Hippocamp', 'Naiad']}
]
Accessing one of the moons of Neptune
console.log(myFavoritePlanets[2].moons[4])
Simple function called
greeting
that shows the following message:
Hello, my name is john
using
alert
built-in function. Then we can call the function by typing the function name followed by parenthesis:
greeting();
function greeting() {
    alert('Hello, my name is john');
}
greeting();
The same function with more flexibility. We pass the parameter
name
to the function, so it can be called for different users. When we call the function, we need to pass a string, as an argument
greeting('John');
function greeting(name) {
    alert('Hello, my name is ' + name);
}
greeting('John');
The same function with several parameters. We added another parameter
age
to the function
function greeting(name, age) {
    alert('Hello, my name is ' + name + ' and I am ' + age + ' years old');
}
greeting('John', 25);
The following function returns value, so that we can use the function in expressions
function doubleMe(number) {
    return 2*number;
}
console.log(3*doubleMe(50));  // 300
We can assign default values to parameters if they are not passed when the function gets called
function aboutMe(profession = 'student', name, city = 'Tbilisi' ) {
    console.log('I am a ' + profession + ' from ' + city + ' and my name is ' + name);
}
aboutMe(undefined, 'Valeri');
There are several built-in functions to interact with the user:
alert('Text message');
prompt('What is your name?');
confirm('Do you want to learn JavaScript')
Function
alert
shows a text message
alert('Text message');
Function
prompt
asks the user to input a text. It can be an answer for the question that the function asks the user
prompt('What is your name?');
The function has another optional parameter, which is a default value of the input
prompt('What is your favorite programming language?', 'JavaScript');
Function
confirm
asks the user and waits for the user to accept or cancel
confirm('Do you want to learn JavaScript')
Simple example of the three interactive functions together:
if (confirm('Do you want to learn programming?')) {
    let language = prompt('Choose your favorite programming language: JS, Python, PHP', 'JS');
    alert('You will be redirected to ' + language + " tutorial's page");
    window.location.href = 'https://w3schools.com/'+language;
} else {
    alert('Go waste your time with playing video games')
}
Results: 62