JavaScript Basics
Set of JavaScript basic syntax to add, execute and write basic programming paradigms in Javascript
On Page Script
Adding internal JavaScript to HTML
External JS File
Adding external JavaScript to HTML
Functions
JavaScript Function syntax
DOM Element
Changing content of a DOM Element
Output
This will print the value of a in JavaScript console
Conditional Statements
Conditional statements are used to perform operations based on some conditions.
If Statement
The block of code to be executed, when the condition specified is true.
If-else Statement
If the condition for the if block is false, then the else block will be executed.
Else-if Statement
A basic if-else ladder
Switch Statement
Switch case statement in JavaScript
Iterative Statements (Loops)
Iterative statement facilitates programmer to execute any block of code lines repeatedly and can be controlled as per conditions added by the programmer.
For Loop
For loop syntax in javascript
Example:
While Loop
Runs the code till the specified condition is true
Do While Loop
A do while loop is executed at least once despite the condition being true or false
Strings
The string is a sequence of characters that is used for storing and managing text data.
charAt method
Returns the character from the specified index.
concat method
Joins two or more strings together.
index of method
Returns the index of the first occurrence of the specified character from the string else -1 if not found.
match method
Searches a string for a match against a regular expression.
replace method
Searches a string for a match against a specified string or char and returns a new string by replacing the specified values.
search method
Searches a string against a specified value.
split method
Splits a string into an array consisting of substrings.
substring method
Returns a substring of a string containing characters from the specified indices.
Arrays
The array is a collection of data items of the same type. In simple terms, it is a variable that contains multiple values.
variable
Containers for storing data.
concat method
Joins two or more arrays together.
indexOf method
Returns the index of the specified item from the array.
join method
Converts the array elements to a string.
pop method
Deletes the last element of the array.
reverse method
This method reverses the order of the array elements.
sort method
Sorts the array elements in a specified manner.
toString method
Converts the array elements to a string.
valueOf method
returns the relevant Number Object holding the value of the argument passed
Number Methods
JS math and number objects provide several constant and methods to perform mathematical operations.
toExponential method
Converts a number to its exponential form.
toPrecision method
Formats a number into a specified length.
toString method
Converts an object to a string
valueOf method
Returns the primitive value of a number.
Maths Methods
ceil method
Rounds a number upwards to the nearest integer, and returns the result
exp method
Returns the value of E^x.
log method
Returns the logarithmic value of x.
pow method
Returns the value of x to the power y.
random method
Returns a random number between 0 and 1.
sqrt method
Returns the square root of a number x
Dates
Date object is used to get the year, month and day. It has methods to get and set day, month, year, hour, minute, and seconds.
Pulling Date from the Date object
Returns the date from the date object
Pulling Day from the Date object
Returns the day from the date object
Pulling Hours from the Date object
Returns the hours from the date object
Pulling Minutes from the Date object
Returns the minutes from the date object
Pulling Seconds from the Date object
Returns the seconds from the date object
Pulling Time from the Date object
Returns the time from the date object
Mouse Events
Any change in the state of an object is referred to as an Event. With the help of JS, you can handle events, i.e., how any specific HTML tag will work when the user does something.
click
Fired when an element is clicked
oncontextmenu
Fired when an element is right-clicked
dblclick
Fired when an element is double-clicked
mouseenter
Fired when an element is entered by the mouse arrow
mouseleave
Fired when an element is exited by the mouse arrow
mousemove
Fired when the mouse is moved inside the element
Keyboard Events
keydown
Fired when the user is pressing a key on the keyboard
keypress
Fired when the user presses the key on the keyboard
keyup
Fired when the user releases a key on the keyboard
Errors
Errors are thrown by the compiler or interpreter whenever they find any fault in the code, and it can be of any type like syntax error, run-time error, logical error, etc. JS provides some functions to handle the errors.
try and catch
Try the code block and execute catch when err is thrown
Window Methods
Methods that are available from the window object
alert method
Used to alert something on the screen
blur method
The blur() method removes focus from the current window.
setInterval
Keeps executing code at a certain interval
setTimeout
Executes the code after a certain interval of time
close
The Window. close() method closes the current window
confirm
The window.confirm() instructs the browser to display a dialog with an optional message, and to wait until the user either confirms or cancels
open
Opens a new window
prompt
Prompts the user with a text and takes a value. Second parameter is the default value
scrollBy
scrollTo
Scrolls the document to the specified coordinates.
clearInterval
Clears the setInterval. var is the value returned by setInterval call
clearTimeout
Clears the setTimeout. var is the value returned by setTimeout call
stop
Stops the further resource loading
Query/Get Elements
The browser creates a DOM (Document Object Model) whenever a web page is loaded, and with the help of HTML DOM, one can access and modify all the elements of the HTML document.
querySelector
Selector to select first matching element
querySelectorAll
A selector to select all matching elements
getElementsByTagName
Select elements by tag name
getElementsByClassName
Select elements by class name
Get Element by Id
Select an element by its id
Creating Elements
Create new elements in the DOM
createElement
Create a new element
createTextNode
Create a new text node