JavaScript Informational: Best of the Basics
Best of the Basics: JavaScript Informational

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.
Congratulations!
- 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.
An Important Note
When assigning a type of data to a variable—be it a string, number,
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
- 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.
-
Declared: var myVar; // But 'NOT defined'.
- Defined: myVar = ''; // The = assignment operator will assign a value and define the variable.
This really is important stuff, I promise! If you're bored ... it will pass (or try accounting (j/k!!)). This really is just the basics. You can't build the house until you learn to swing the hammer (and other cool sayings...) :)
And without further ado...
Introducing... Your Data Types!
The typeof operator (or, "command") will return lowercase strings for each of the data types listed below.
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
Comparators < === > simply compare two data items. For example, 3 == 3 compares two 3s.
The biggest thing here to learn and remember is that there are two ways to compare data items.
-
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
Best practices can at times be subjective; choose your preference.
But! Be able to code in the style of the project you're working in.
Optional: Curly Braces (grouping bits of code)
- 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
Same holds true for semicolonsat the end of a line;
They are optional, but because there are some gotchas you have to watch out for,
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!
For these examples, understand that Math.random()
returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1).
Ergo,
Math.random() <= 0.999999999999999999 and
Math.random()*2 <= 1.9999999999999999
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
Do NOT put sensitive or similar information in comments.
<u>
Copy/Paste
A couple approaches I use when I paste things into anywhere—and then begin experiencing issues— (indicating it was due to the paste):
- 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
Spending one to two days digging deep into these will allow you to better comprehend some even more powerful features
such as Layout mode, Flexbox, and CSS Grid.
Padding, Margin
Border; Outline
New HTML Element Types: YMMV (your miles may vary)
As you grow with JavaScript, MDN and StackOverflow will typically become your primary go-tos.
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
Familiarity through repetition is a big part of the initial learning curve.
If you can build yourself any little "thing" that does something with HTML, CSS, and JavaScript, you'll have created a miracle. Build on it from there.
Acronysms
You will also come across this with i18n for internationalization (coding for other languages).
These types of "acronysms" are explained here: https://en.wikipedia.org/wiki/Acronym#Other_conventions
FYI: "acronysms" is a made-up word; although there are 17k Google results. I enjoy research. :D
Learning Resources
One of the best coding walkthrough and challenge sites for learning.
free Code Camp.org
Good simple, progressive, code exercises
Good simple, progressive, code exercises
An additional learning resource for progressing your skills is Flash Cards such as https://ankiweb.net/
ReplyDeleteAnkiWeb has a tried-and-true algorithm for better brain-burning---it will algorithmically adjust your cards based on your recall level.
An excellent list of suggestions for honing your web development skills (which prompted my addition of flash cards) provided by Tyler S. Lemke @ https://www.linkedin.com/in/tylerslemke/ and can be found on LinkedIn at: https://www.linkedin.com/feed/update/urn:li:activity:6625201378568523776/
ReplyDelete