JavaScript can be used to write into an HTML element. This is very useful for showing the results of functions written in JavaScript. Click the button below to change the text of this paragraph.
JavaScript can be used to solve math equations. This makes it useful for web applications that require calculations to be handled. For example, JavaScript can be used to evaluate the following expression: 100 + 50. Click the button below to calculate 100 + 50
JavaScript can be used to display Dates. According to W3Schools website, "By default, JavaScript will use the browser's time zone and display a date as a full text string". This can be seen in the following example:
Everytime you click the button, JavaScript will display the current time according to the browsers time zone at the moment the button is clicked.
There are various things that can be done with strings using JavaScript. Something like a zip code field might require 5 numbers to be inputted, no more and no less. This is where the length property can come in handy. Enter 5 characters in the textbox below. If there is less than 5 characters an alert will pop saying "PLEASE ENTER 5 CHARACTERS ONLY!". If more than 5 characters are entered an alert will pop saying "PLEASE ENTER 5 CHARACTERS ONLY!". If 5 characters are entered an alert will pop saying "Congratulations, you follow directions!"
Elements in an array can be joined together into a string using JavaScript. This can be done using the join() method. Click the button below to take the following array elements: fruits = ["Apple", "Orange", "Strawberry", "Grape", "Cherry"]; And join them together with "-" as the separator between the elements.
JavaScrip Classes are extremely useful because they can be used to create objects. It can be thought of as a template for objects. Lets take for example a class that can be used to store information about a student. The student properties such as name, age, and grade (among other things). To keep it simple I will keep it at just name, age, and grade. Enter the student name, age, and grade in the textboxes below. Clicking the button will generate a student object from a student class.
JSON is useful for communicating with server side applications. This is because of its ability to convert objects into strings and also strings back into objects. Strings are required to be in JSON syntax though. According to W3Schools, JSON syntax is name/value pairs that consist "of a field name (in double quotes), followed by a colon, followed by a value (also in double quotes)". Syntax looks like the following: "name" : "John" I will use JSON to convert the following JavaScript Object into a JSON string using JASON.stringify(). JavaScript object: const obj = { firstName: "Jeff" lastName: "Smith" age: 17 grade: 12 Click the button below to turn the JavaScript object into a JSON string
Another one of JavaScipts string methods is the slice() Method. This can be used to slice strings so only the desired text displays. Click the button below to display the current time.
Click to display Date
Click to slice Date
Yet another one of JavaScripts string methods is the replace() Method. This can be used to replace certain words within a string with a different word. Take for example the following sentence.
Peter Piper picked a peck of pickled peppers.
Sometimes in JavaScript, one would want to display not the current date, but the date from a month prior. This can be done using the JavaScript setMonth() method and the getMonth() method. Click the button below to display the date from one month prior to today's date.
Click for last months date!