Javascript: Welcome To The Second Class of 0 To 0.1 Javascript Prepared By: Abdul Qadir
Javascript: Welcome To The Second Class of 0 To 0.1 Javascript Prepared By: Abdul Qadir
window.alert()
document.write()
document.write() function is convenient for testing purposes. It's primary
function is to delete all HTML whenever the HTML document is loaded. You
have to be very careful with this JavaScript output method.
console.log(12 + 3);
Using innerHTML
To access an HTML element, JavaScript can use the
document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines
the HTML content:
The prompt() method displays a dialog box that prompts the visitor for input.
A prompt box is often used if you want the user to input a value before
entering a page.
Note: When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value. Do not overuse this
method, as it prevents the user from accessing other parts of the page until
the box is closed.
The prompt() method returns the input value if the user clicks "OK". If the
user clicks "cancel" the method returns null.
Syntax
prompt(text, defaultText)
Confirm()
The confirm() method displays a dialog box with a specified message, along
with an OK and a Cancel button.
A confirm box is often used if you want the user to verify or accept
something.
Note: The confirm box takes the focus away from the current window, and
forces the browser to read the message. Do not overuse this method, as it
prevents the user from accessing other parts of the page until the box is
closed.
The confirm() method returns true if the user clicked "OK", and false
otherwise.
Return A Boolean, indicating whether "OK" or "Cancel" was clicked in
Value: the dialog box:
Quick practice Question write all display methods and input methods and
see which appear when
Undefined
Undefined is the value given to variables that have not been assigned a value.
We’ve already seen it used earlier in this chapter when variables are declared
without being assigned a value. It can also occur if an object’s property doesn’t
exist or a function has a missing parameter. It is basically JavaScript’s way of
saying "I can’t find a value for this."
Null
Null means “no value”. It can be thought of as a placeholder that JavaScript uses
to say "there should be a value here, but there isn’t at the moment."
If this reminds you a lot of undefined then this is because they are both “nonvalue”
values, meaning they are similar, but behave slightly differently. For
example, if you try to do sums with them:
javaScript Hoisting
Hoisting is JavaScript's default behavior of moving declarations to the top.
Scope
Scope is an important concept in programming. It refers to where a constant or
variable is accessible by the program. There are two common scopes that are often
referred to in programs: global scope and local scope.
ES2015 introduced two important new JavaScript keywords: let and const.
These two keywords provide Block Scope variables (and constants) in JavaScript.
Before ES2015, JavaScript had only two types of scope: Global Scope and Function
Scope.
Global Scope
Variables declared Globally (outside any function) have Global Scope.
Example
function myFunction() {
Function Scope
Variables declared Locally (inside a function) have Function Scope.
Example
function myFunction() {
Local variables can only be accessed from inside the function where they are
declared.
Example
var x = 2;
Example
let x = 2;
The variable is in a "temporal dead zone" from the start of the block until it is
declared:
let x = 2; // Allowed
{
let x = 3; // Allowed
}
{
let x = 4; // Allowed
}
Javasript const variable
in contrast, using const means you can’t reassign the variable to another value.
That means that if a variable is assigned to a primitive data type, then the value
can’t be changed, and will result in an error if you attempt to:
const x = 2; // Allowed
const x = 3; // Not allowed
x = 3; // Not allowed
var x = 3; // Not allowed
let x = 3; // Not allowed
{
const x = 2; // Allowed
const x = 3; // Not allowed
x = 3; // Not allowed
var x = 3; // Not allowed
let x = 3; // Not allowed
}
String Properties and Methods
let name = "helloWorld"
console.log(name.length) // retrieve the name variable’s length property
//upercase string
console.log(name.toUpperCase())
//lowercase string
console.log(name.toLowerCase())
console.log(name.startsWith('h'))
//return true or false
console.log(name.endsWith('d'))
//return true or false