Best of the Basics: JavaScript Informational

So you wanna control your browser, ehhh…?
Start with a Solid Base
- Q: Why would you want to create a variable?
- A: Variables are a part of what allows us to control “app”lications—such as your web browser.
For instance, most browsers allow you to open their “developer tools” using the F12 key.
If you click the “Console” tab at the top of those tools, and type the following at the > prompt:
var myvar = “hello”; and press enter.
Once you assign information to a variable, you can then access that information—by referring to your variable’s name—with other code.
For instance, you can assign numbers to two different variable names, and then add those two variables together.
In doing that, you will have basically just created the ‘addition’ function on a calculator. Bet you didn’t know you’d be doing that today.
- Q: What kind of information can you put in a variable?
- A:
These are called data types, and they refer to things like strings and numbers.
Previously you added a string, “hello”.
Then just now you mentally added a number, which you could do by typing var myvar = 34; and pressing enter.
which just means the base objects themselves cannot be altered (for instance, the number 3 will always be the number 3,
although your myvar reference to the number 3 may change).
These objects can also be based on the seven primitive data types, and as a reference (variable assignment) to those primitive data types, their values can be changed.
An Important Note
or other—it should never be changed to a different data type.
If you initially assign var myvar = 3; you should
not try to set it later in the code to be myvar = “3”;
(which is a string—not a number).
The Base
- Data is one of the underlying fundamentals consistent across every application.
- Variables allow us to handle that data.
- Data Types allow us to keep things organized.
It’s All About the Data
Understanding Data Types
When you get code from other developers, it will grow harder to keep track of all your variables.
With every new feature you add to your web app, it will grow harder to keep track of all your variables.
- To keep track of your variables, you need to be able to check them.
- You’ll need to see what type of data is in them (if any), or if they even exist (i.e. if they’ve been created).
- Checking data types helps us control the flow of our data.
If the variable hasn’t been created and doesn’t exist at all, it is undeclared (and will throw a Reference error in the browser).
If the variable has been created but hasn’t been assigned anything, it is undefined (which you can—and will—check for regularly).
- Declared: var myVar; // But ‘NOT defined’.
- Defined: myVar = ”; // The = assignment operator will assign a value and define the variable.
And without further ado…
Introducing… Your Data Types!
As noted on MDN,
these typeof return strings can be one of the following:
typeof | Result |
---|---|
String | “string“ |
Number | “number“ |
Boolean | “boolean“ |
Undefined | “undefined“ |
Null | “object“ |
Function object | “function“ |
Symbol (new in ECMAScript 2015) | “symbol“ |
BigInt (new in ECMAScript 2020) | “bigint“ |
Any other object | “object“ |
Some Clarity
Number, String, Boolean, Object | ||
|
||
Undefined | ||
|
||
Null | ||
|
||
Symbol, BigInt | ||
|
||
Don’t change data types !!! |
||
|
To the Code!
Comparing … Stuff
-
Compare the Values (between two variables) == and !=
Use 2 character signs to just compare the values.
3 == ‘3’ === true. -
Compare the Values AND the Data Types === and !==
Use 3 character signs to compare the values AND the data types.
3 === ‘3’ === false because one is a number and one is a string. - Best practice is to always use === (especially being that variables should never change what ‘type‘ of values they hold).
Common algebra comparison operators. | < | <= | > | >= |
Check for value only | == | != | ||
Check value and data type | === | !== |
Coding Styles; Standards vs. Preferences
Additionally, different industries may have different coding styles.
But! Be able to code in the style of the project you’re working in.
Optional: Curly Braces (grouping bits of code)
{ curly braces }.
When you come across these optional braces… Best practice is to “always” wrap your statements with curly braces.
However, consistency would dictate—because you have to use them when you have two or more lines of code to run—they should always be used.
- Coding Examples:
-
// This will work, and some developers prefer this.
if (alwaysWrapWithBraces === true)
console.log(“One-liners should be wrapped up for consistency.”);
-
// Best practice (subjective)
if (alwaysWrapWithBraces === true) {
console.log(“Consistency is a valued trait in web development.”);
}
Optional: Semicolons
and for which merely introduce more potential entry points for bugs, as with the argument for curly braces, it’s best to just always include them.
Parentheses Matter!
there are times when you need to tell the browser in which order things need to be done.
returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1).
Ergo,
Math.random()*2 <= 1.9999999999999999
Parentheses Placement | Possible Values |
---|---|
Math.floor(Math.random() * 2 – 1) | // -1 – 0 |
Math.floor(Math.random() * (2 – 1)) | // 1 |
Math.floor(Math.random() * (3 – 1)) | // 0 – 1 |
Math.floor(Math.random() * (3 – 1) + 1) | // 1 – 2 |
Math.floor(Math.random() * 3) | // 0 – 2 |
Miscellaneous Tidbits
Naming Conventions
-
HTML <tags></tags>: The general concensus is that <html> tags are “almost always” lowercase.
However—technically—it doesn’t matter. But! I look at my code from 2005 and it’s <YELLING AT ME>!
I’ve preferred lowercase ever since that JUMPED out at me one day.
<!DOCTYPE html> is a good exception to this rule. -
HTML ID attributes should simply avoid using kebab-case, and instead use either camelCase or PascalCase.
After many years of seeing differently, this took some major thought adjustment, but my IDs from here-on-out will be PascalCase (aka, TitleCase). - CSS .class-names should be written and named using kebab-case (all-lowercase-with-hyphens).
- JavaScript variable and function names should be written and named using camelCase (not to be confused with TitleCase (or PascalCase)).
Comments are Public
Comments are hidden from the browser’s “viewing” page, but the comments are still accessible if you view the page source (Ctrl|Cmd-U) or explore in your browser’s Developer Tools.
<u>
especially if the site’s <a> link styles are emphasized in some manner
so as not be confused with the standard <u> underline.
Understand the context in which you’re working in.
Be aware of your development environment and a site’s own coding styles.
Copy/Paste
Unfortunately it will not “always” remove all hidden characters.
If problems with a certain section of text in your code persists and is proving challenging,
you can also try pasting into a Word Processor, most of which provide the option to “show hidden characters.”
- Delete a chunk of the pasted information, test to see if the issue is still there, then ‘undo’ the delete. Repeat until the error goes away, and narrow it down.
- When you use you left-right arrow keys to scroll through text, you’ll see the cursor scroll happily along.
Hidden characters, however, will cause the cursor to ‘skip a beat’ as it ‘goes over’ the hidden character.
When you see this skip happen, you know there’s something in there, and you can trim it out.
Block vs. Inline vs. Inline-Block Elements
Understanding “block” vs. “inline” vs. “inline-block” elements is crucial to front-end web development.
MDN has already done an excellent job of explaining these concepts.
such as Layout mode, Flexbox, and CSS Grid.
Padding, Margin
Border; Outline
- Outlines never take up space, as they are drawn outside of an element’s content.
- According to the spec, outlines don’t have to be rectangular, although they usually are.
- A `border: 5px solid red;` will visibly move/shift adjacent content.
- An `outline: 5px solid red;` is more of an overlay and will not affect the contents.
New HTML Element Types: YMMV (your miles may vary)
The MDN docs should be required reading (or at least required skimming).
Every feature has its own chart. MDN especially includes notes on limitations of use, and all the charts usually include mobile browser support.
Although MDN may appear to be more cryptic early on, you will become more comfortable with it as you progress.
Tools
- HTML Validation | https://validator.w3.org
- CSS Validation | http://jigsaw.w3.org/css-validator/
Cheat Sheets
- HTML5 cheat sheet | https://websitesetup.org/html5-cheat-sheet/
- Windows keyboard shortcuts | https://www.google.com/search?q=windows+keystrokes
A few common locations for project files
Local: | Location on Computer/Drive | |
Windows | [Desktop, My Documents, etc.] | |
Mac | [Desktop, Documents, etc.] | |
Linux | [/var/www/html/] | |
Remote: | ||
GitHub | Provides free public repositories | |
A “repository” is a virtual folder that stores your project’s files. You can have multiple repositories; each with its own project files. |
||
GitHub Gists | More for code snippets | |
BitBucket | Like GitHub, but isntead of free public repositories, BitBucket provides free private repositories | |
CodePen | Free online text editor with live preview pane | |
Others… |
The #1 essential tip for progressing your skills
The more little projects you challenge yourself to do, the more ‘life training badges’ you’ll garner.
Acronysms
Learning Resources
Good simple, progressive, code exercises
Additional Learning Resources.
Brought to you by
Keith D Commiskey
https://keithdc.com
Leave a Reply
You must be logged in to post a comment.