JavaScript Notes 1
JavaScript Notes 1
Constant
const dialog = 'Sam looked up, and said "hello, old friend!", as
Max walked in.';
const imperative = "Don't do that!";
Objects
Dates
Arrays
// array literals
const arr1 = [1, 2, 3]; // array of numbers
const arr2 = ["one", 2, "three"]; // nonhomogeneous array
const arr3 = [[1, 2, 3], ["one", 2, "three"]]; // array containing
arrays
// accessing elements
arr1[0]; // 1
arr1[2]; // 3
arr3[1]; // ["one", 2, "three"]
// array length
arr1.length; // 3
// Array constructor (rarely used)
const arr5 = new Array(); // empty array
const arr6 = new Array(1, 2, 3); // [1, 2, 3]
const arr7 = new Array(2); // array of length 2 (all elements
undefined)
const arr8 = new Array("2"); // ["2"]
JavaScript in Browser
WINDOW OBJECT
The browser represents each window or tab using a window object. The location property of
the window object will tell you the URL of the current page.
PROPERTIES
location http://www.javascriptbook.com/
DOCUMENT OBJECT
The current web page loaded into each window is modeled using a document object
PROPERTIES
URL: http://www.javascriptbook.com/
lastModified 09/04/2014 15:33:37
title: Learn JavaScript & jQuery -A book that teaches youvin a nicer way
PROPERTIES
Properties describe characteristics of the current web page (such as the title of the page).
METHODS
Methods perform tasks associated with the document currently loaded in the browser (such
as getting information from a specified element or adding new content).
EVENTS
You can respond to events, such as a user clicking or tapping on an element.
HOW A BROWSER SEES A WEB PAGE
1: RECEIVE A PAGE AS HTML CODE
2: CREATE A MODEL OF THE PAGE AND STORE IT IN MEMORY
3: USE A RENDERING ENGINE TO SHOW THE PAGE ON SCREEN
You may see JavaScript in the HTML between opening <script> and closing </script> tags
(but it is better to put scripts in their own files).
<script>document.write(' <h3>Welcome !</h3>');
getElementById
<html lang="en">
<head>
<title>getElementById example</title>
<script>
function changeColor(newColor) {
const elem = document.getElementById('para');
elem.style.color = newColor;
}
</script>
</head>
<body>
<p id="para">Some text here</p>
<button onclick="changeColor('blue');">blue</button>
<button onclick="changeColor('red');">red</button>
</body>
</html>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="parent-id">
<p>hello word1</p>
<p id="test1">hello word2</p>
<p>hello word3</p>
<p>hello word4</p>
</div>
<script>
const parentDOM = document.getElementById("parent-id");
const test1 = parentDOM.getElementById("test1");
// throw error
// Uncaught TypeError: parentDOM.getElementById is not a function
console.log(parentDOM);
console.log(test1);
</script>
</body>
</html>
functions
anonymous function : Anonymous Function is a function that does not have any name
associated with it. Normally we use the function keyword before the function name to
define a function in JavaScript, however, in anonymous functions in JavaScript, we use
only the function keyword without the function name.
Example 1
<script>
var greet = function () {
console.log("Welcome to GeeksforGeeks!");
};
greet();
</script>
Example 2
<script>
var greet = function (platform) {
console.log("Welcome to ", platform);
};
greet("GeeksforGeeks!");
</script>
Example 3
In this example, we pass an anonymous function as a callback function to the
setTimeout() method. This executes this anonymous function 2000ms later.
<script>
setTimeout(function () {
console.log("Welcome to GeeksforGeeks!");
}, 2000);
</script>
var hotel = {
name: 'Quay',
rooms : 40,
booked: 25,
checkAvailability: function() {
return this.rooms - this.booked;
}
};
var hotel = {
name: 'Quay',
rooms: 40,
booked: 25,
checkAvailability: function () {
return this.rooms - this.booked;
}
};
console.log(hotel.checkAvailability());
Output : 15
Built-in Objects