JavaScript program is a list of programing statements that are to be executed by a computer that are composed of values, operators, expressions, keywords, and // comments, these are executed one by one or same order as they are written. When writing a line of codes it is best to avoid lines longer then 80 characters, if it doesnt fit on one line break it after an operator. Keep in mind JavaScript is case sensitive
getElementById() this is inserted between a script tag and you may write as many scripts as you would like in HTML. It can be placed in the body or head tag or both as well. JavaScript can be place in external files.
document.getElementById("demo").innerHTML = "Hello JavaScript";document.getElementById("demo").style.fontSize = "16px";document.getElementById("demo").style.display = "none";document.getElementById("demo").style.display = "block"; document.getElementById("demo").innerHTML = "My First JavaScript";Not required but highly recommended because it separtes JavaScript statements at the end of each executable statement and also allow multiple statements.
Fixed values are literals which are numbers that written with or without decimals and extra large or extra small numbers can be written with scientific exponent notation or strings of text written within double or single quotes ” ” or ‘ ’.
Variable values are variables that store data values, for instance; var, let and const to declare variables. The = is to assign values to variables these are * allowed in any program.
Are arithmetic operators (+ = * /) to compute values (called operands). Two number can be literals, variables or expressions
The let will tells the browser to create variables and var will tell the browser to create variables, both produce same result. Which hold different data types numbers, strings, objects and more.
// comments
/*
*/
Block of JavaScript code that can be executed, event is pushing a button, can be placed in the <head> to push a button in the <body>. You can place as many as you would like but external scripts cannot contain <script>
Practical when same code for many different web pages. It separates HTML and code which makes it easier to read and manage. Can also speed up page loads. These can be referenced in 3 different ways: full URL, file path, or no path to line to notes.js
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}'''js
<script src="notes.js"></script>'''
This defines the HTML element and property defines the HTML content. Cahanging the innerHTML property of an HTML element is a comman way to display data in your HTML
document.getElementById(id) method.
alert(5+6)
There are no print objects or print methods and cannot access output devices from JavaScript, exceptions window.print() in the browser to bring the content of the current window
Always declare variables with const unless the value of the variable changes and must be assigned a value when declared. This is used for new array, object, function or RegExp. It also does not define a constant value defines constant reference. And changes elements of constant array and properties of constant object.
const price1 = 5;
const price2 = 6;
let total = price1 + price2;+ and += can be used to add concatenate strings. Strings can have ‘ ‘ or “ “ as long as they do not match the quotes surrounding the string.
const states = [“Florida, “California”, “Illinois”]
const person = {firstName:”Miranda”, lastName:”Olson”, age:35, eyeColor:"brown”};
objectName. propertyName or objectName[‘propertyName’]
Objects can also have methods which are actions that can be preformed on objects and stored in properties as function definitions. Also avoid string, number and boolean objects slow down execution speed
objectName.methodName() or name = person.fullName();
Things that should be done every time loading a page or closing and action that should b preformed when clicking a button. Content that should be verified when a user inputs data.
let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = txt.length;let str = “Orchid, Hibiscus, Rose ;
let part = str.slice(7, 13);let str = “Orchid, Hibiscus, Rose” ;
let part = str.slice(-12, -6);substring (start, end) start and end values less than 0 are treated as 0 in substring() and sustr(start, length) second parameter specifies the length of the extracted part.
Strings are immutable and strings cannot be changed only replaced.This returns a new string and replaces only the first match. Make sure to replace all matches us /g flag set.
let text = "Please visit Microsoft!";
let newText = text.replace("Microsoft", "W3Schools");let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace(/Microsoft/g, "W3Schools");let text1 = "Hello World!";
let text2 = text1.toUpperCase();let text1 = "Hello World!"; // String
let text2 = text1.toLowerCase(); // text2 is text1 converted to lowerlet text1 = " Hello World! ";
let text2 = text1.trim();let numb = 8;
let text = numb.toString();
let padded = text.padStart(7,”0”);let text = “8”;
let text = numb.toString();
let padded = text.padEnd(7,”0”);let text = "HELLO WORLD";
let char = text.charAt(0);charCodeAt (position) returns the unicode of the character at a specified index in a string (returns a UTF-16 code)
let text = "HELLO WORLD";
let char = text.charCodeAt(0);let text = "HELLO WORLD";
let char = text[0];text.split(","); // Split on commasIf separator is omitted, returned array will contain the whole string in index [0] but if separator is “ “, returned array will be an array of single character
Returns the index of the first occurrence of a specified text in a string but cannot take a powerful search values (reg. expressions)
Searches a string for a specified value and returns the position of the match but cannot take a second start position argument
Returns the index of the last occurrence of a specified text in a string. If second parameter is 18, search starts at position 18 then searches to the beginning of the string.
Back-Tics Syntax instead “ “ define a string. Quotes inside strings use both single and double quotes inside a string
let text = `She’s often called “Miranda”`;// left off on JavaScript Number Methods