Sitemap

Everything about Function | JavaScript Tutorial

1 min readJul 27, 2022

--

An example of understanding how scope in function works?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Function scope example</title>
</head>
<body>
<script>
const x = 1;

function a() {
const y = 2;
output(y)

}

function b() {
const z = 3;
output(z)
}

function output(value) {
const para = document.createElement('p');
document.body.appendChild(para);
para.textContent = `Value: ${value}`;
}
</script>
</body>
</html>

Test Case:

  1. Remove output form function a and function b. and console the output function with all three variables with x,y, and z.
  2. Replace the output(z) with y and output(y) with z and the console function.

Get Revive Coding’s stories in your inbox

Join Medium for free to get updates from this writer.

Create a random name generator from an Array of names….

const names = ['Chris', 'Li Kang', 'Anne', 'Francesca', 'Mustafa', 'Tina', 'Bert', 'Jada']const body = document.querySelector("body")const para = document.createElement('p');const section = document.createElement('div')body.appendChild(section)function chooseName() {
newname = names[Math.floor(Math.random()* names.length)]
para.innerHTML = newname;
}
chooseName() section.innerHTML = ' ';section.appendChild(para);

--

--

Revive Coding
Revive Coding

Written by Revive Coding

Revive Coding is a place where you learn Web Development. Here we teach you, art of becoming Full Stack Web Developer and ready for your Dream Job.