JavaScript Document
JavaScript Document
<script src="myScript.js"></script>
1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object
Console log():
The log() method writes (logs) a message to the console.
The log() method is useful for testing purposes.
console.log("Hello world!");
HTML DOM Document write():
The write() method writes directly to an open (HTML) document
stream.
The write() method deletes all existing HTML when used on a loaded
document.
document.write("Hello World!");
Window alert():
The alert() method displays an alert box with a message and an OK
button.
The alert() method is used when you want information to come
through to the user.
alert("Hello! I am an alert box!!");
JavaScript Variables:
Using var
Using let
Using const
Automatically
When to Use var, let, or const?
1. Always declare variables
2. Always use const if the value should not be changed
3. Always use const if the type should not be changed (Arrays and
Objects)
4. Only use let if you can't use const
5. Only use var if you MUST support old browsers.
What is Good?
1. let and const have block scope.
2. let and const can not be redeclared.
3. let and const must be declared before use.
4. let and const does not bind to this.
5. let and const are not hoisted.
JavaScript String:
A JavaScript string stores a series of characters like "Sample Text".
A string can be any text inside double or single quotes.
The first character is in position 0, the second in 1, and so on.
var name = "Volvo XC60";
JavaScript Number:
JavaScript has only one type of number.
Numbers can be written with, or without, decimals:
let x = 3.14; // A number with decimals
let y = 34; // A number without decimals
JavaScript Booleans:
JavaScript Booleans can have one of two values: true or false.
let x = false;
JavaScript BigInt:
JavaScript BigInt variables are used to store big integer values that are
too big to be represented by a normal JavaScript Number.
var x = BigInt("123456789012345678901234567890");
Prompt Box:
A prompt box is often used if you want the user to input a value before
entering a page.
When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks
"Cancel" the box returns null.
EXERCISE: Check if the value entered through prompt is even number
or not
JavaScript Arithmetic Operators:
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement
JavaScript Assignment Operators:
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
The += Operator
The Addition Assignment Operator adds a value to a variable.
let text = "Hello"; text += " World";
The -= Operator
The Subtraction Assignment Operator subtracts a value from a variable.
let x = 10; x -= 5;
The *= Operator
The Multiplication Assignment Operator multiplies a variable.
let x = 10; x *= 5;
The /= Operator
The Division Assignment Operator divides a variable.
let x = 10; x /= 5;
The %= Operator
The Remainder Assignment Operator assigns a remainder to a variable.
let x = 10; x %= 5;
JavaScript Comparison Operators:
== equal to a == b
!= not equal a != b
|| or (x == 5 || y == 5) is false
Math methods:
Math.random()
Math.floor()
Math.sqrt()
Math.min(23,88,01)
Math.max(839,347,99,2)
Math.pow(x,y) x raise to y
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1
(exclusive). Math.random() always returns a number lower than 1.
Math.random();
Math.floor()
The Math.floor() method rounds a number DOWN to the nearest
integer.
Math.floor(1.6);
Math.sqrt()
The Math.sqrt() method returns the square root of a number.
Math.sqrt(64);
Math.min()
The Math.min() method returns the number with the lowest value.
Math.min(0, 150, 30, 20, 38);
Math.max()
The Math.max() method returns the number with the highest value.
Math.max(0, 150, 30, 20, 38);
Math.pow()
The Math.pow() method returns the value of x to the power of y.
Math.pow(4, 3);
JavaScript parseInt():
The parseInt method parses a value as a string and returns the first
integer.
parseInt("10");
JavaScript inNaN():
In JavaScript NaN is short for "Not-a-Number".
The isNaN() method returns true if a value is NaN.
The isNaN() method converts the value to a number before testing it.
isNaN('Hello');
JavaScript toString():
The toString() method returns a string as a string.
The toString() method does not change the original string.
method can be used to convert a string object into a string.
toString('Hello');
Document getElementsByTagName():
The getElementsByTagName() method returns a collection of all
elements with a specified tag name.
The getElementsByTagName() method returns an HTMLCollection.
The getElementsByTagName() property is read-only.
getElementsByTagName("*") returns all elements in the document.
document.getElementsByTagName(tagname);
Modifying attribute
1. src:
The src property sets or returns the value of the src attribute of a script.
The src attribute specifies the URL of an external script file.
document.getElementById("myScript").src;
2. setAttribute:
The setAttribute() method sets a new value to an attribute.
If the attribute does not exist, it is created first.
element.setAttribute(name, value);
3. getAttribute:
The getAttribute() method returns the value of an element's attribute.
element.getAttribute(name);
4. removeAttribute:
The removeAttribute() method remove the attribute from an element.
element.removeAttribute(name);
EXERCISE : Check whether the number is even or odd
EXERCISE : Check whether input is a number or not
EXERCISE : Find the largest of the 2 numbers
EXERCISE : Find the largest of the 3 numbers
EXERCISE : Check if a year is a leap year or not