Programming/JavaScript/General Syntax
.js
General Language Notes
- String single and double quotes are interchangeable.
- Semi-colons required.
- Variable casing: camelCase.
- Variables are loosely typed.
- Scope controlled at variable creation.
Comments
Inline Comments
// This is an inline comment.
Block Comments
/** * This is a block comment. * Comment line 2. * Another block comment line. */
Basic Input and Output
Basic Output
The following will output a single line:
console.log('Hello World!');
The following will output multiple lines:
console.log('This is a test.'); console.log('This is line #2'); console.log('Outputting 3 lines total.');
Basic Input
Variables
Variable Declaration
let aBool = true; let bBool = false; let myVar1 = "This is "; let myVar2 = "a string.";
Variable Usage
// Print some variables, as-is. console.log('Printing variable values.'); console.log(aBool); console.log(bBool); // Print two variables concatenated together. console.log(myVar1 + myVar2); // Alternatively. console.log(`${myVar1} ${myVar2}`);
// Combine two variables, then print the result. myVar3 = myVar1 + myVar2; console.log(myVar3);
Booleans
let trueBool = true; let falseBool = false;
Null Values
JavaScript uses the standard null
.
let nullValue = null;
Variable Scope
JavaScript has unique variable scope handling.
Aka, controlling where the variable can and cannot be accessed from.
This is controlled by the combination of how and where a variable is declared from.
The "var" Keyword
The original, default way to declare a variable was through var
. IE:
var myVar;
This has a few quirks compared to most languages. In summary:
- Any
var
declaration is processed before any other code, regardless of where it's declared.- A side-effect is that a variable declared this way can be referenced BEFORE it's actually declared. The result is that it will be "undefined" until it's actually declared.
- The scope of
var
variables automatically includes all inner scopes, such as nested functions.- If the variable is declared outside of any given scope, then it's automatically global.
The result is that larger files can easily have variables with unclear or hard to manage scope. Thus, using var
is not generally recommended.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var for full documentation on exact properties.
The "let" and "const" Keywords
The let
and const
keywords are newer ways to declare variables in JavaScript.
The const
keyword is used to declare an unchanging constant.
The let
keyword is used to create a variable that can change after declaration.
For both of these, the scope is much smaller than var
.
These declarations will only be visible within the code blocks they're defined within. This includes things like if statements. For example:
// Checking scope of variable x and y. function() { let x = 5; // Can see x. y not declared yet. if (true) { let y = 2;
// Can see x and y. } else { // Can see x, but not y. }
// Can see x, but not y. }
Generally, this scope handling is considered safer and more predictable. Thus it's now the preferred way to declare JavaScript variables.