Operator precedence
Operator precedence determines the way in which operators are parsed with respect to each other. Operators with higher precedence become the operands of operators with lower precedence. More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Conditional Statement
If the first character in string is in the set , then return A. If the first character in string is in the set , then return B. If the first character in string is in the set , then return C. If the first character in string is in the set , then return D. Switch function getLetter(s) { let letter; switch (s.charAt(0)) { case’a’: case’e’: case’i’: case’o’: case’u’: return’A’; break; case’b’: case’c’: case’d’: case’f’: case’g’: return’B’; break; case’h’: case’j’: case’k’: case’l’: case’m’: return’C’; break; case’n’: case’p’: case’q’: case’r’: case’s’: case’t’: case’v’: case’w’: case’x’: case’y’: case’z’: return’D’; break; } return letter; }
To retrieve the first letter
var browserType = ‘mozilla’; browserType[0]; Returns: m To retrieve the last letter browserType[browserType.length-1]; Returns: a
How to add quote-marks inside a string in Javascript
However, you can’t include the same quote mark inside the string if it’s being used to contain them. The following will error, as it confuses the browser as to where the string ends:
Put the text cursor into the input text field as soon as the page loads
This line uses the focus() method to automatically put the text cursor into the input text field as soon as the page loads, meaning that the user can start typing their first guess right away, without having to click the form field first. xyzfield.focus();
Script loading strategies
A common problem is that all the HTML on a page is loaded in the order in which it appears. In the internal example, you can see this structure around the code: document.addEventListener(“DOMContentLoaded”, function() { … }); This is an event listener, which listens for the browser’s “DOMContentLoaded” event, which signifies that the HTML body is completely loaded and parsed. In the external example, we use a more modern JavaScript feature to solve the problem, the async attribute, which tells the browser to continue downloading the HTML content once the <script> tag element has been reached. <script src=”script.js” async></script> If your scripts can run independently without dependencies then use async. If your scripts depend on…