JavaScript Fundamentals - Part 1
A variable is a storage container for data.
let message;
message = 'Hello'; //store the string
// or
let message = 'Hello';varletconst
-
vardeclarations are globaly-scoped or function-scoped, and declartions are processed when the script or function starts. This is called hoisting. JavaScript hoists the variable declarion to the top of the function block.varvariables are hoisted to the top of their scope and initialized with a value ofundefined.varvariables can be updated and re-declared within its scope. -
letdeclarations provides block-scoping,{}, and are hoisted to the top of their scope. Unlikevarwhich is initialized asundefined, theletkeyword is not initialized. So if you try to use aletvariable before declaration, you'll get aReference Error.letvariables can be updated but not re-declared. -
constvariables are immutable and can neither be updated nor re-declared.constis block scoped and must be initialized during declaration.
Here is a link to Hacker Noon on why we shouldn't use var anymore.
鈿狅笍 Declaring a variable twice triggers an error- The name must contain only letters, digits, or the symbols
$and_ - The first character must not be a digit
- Case matters!
let,class,return, andfunctionare reserved and cannot be used as variable names- When the name contains multiple words, camelCase is commonly used
"use strict"; defines that JavaScript code should be executed in "strict mode". Strict mode changes previously accepted "bad syntax" into real errors.
"use strict";
x = 3; // This will cause an error because x is not declaredCheck out this guide on MDN web docs on expressions and operators.
A binary operator requires two operands, one before the operator and one after the operator:
operand1 operator operand2 e.g. 3 + 4 or x * y.
A unary operator requires a single operand, either before or after the operator:
operator operand OR operand operator e.g. x++ or ++x.
Concatenation is the process of appending one string to the end of another string. You can check out this tutorial on the 3 ways to concatenate strings in JavaScript. Adding two numbers, will return the sum, but adding a number and a string will return a string.
- Arithmetic Operators e.g.
20%10 = 0orlet a=10; a++; Now a = 11 - Comparison (Relational) Operators e.g.
20!==20 = false - Bitwise Operators e.g.
(10==20 & 20==33) = false - Logical Operators e.g.
(10==20 && 20==33) = false - Assignment Operators e.g.
var a=10; a+=20; Now a = 30 - Special Operators e.g.
(?:) Conditional Ternary Operator returns value based on the condition. It is like if-else.
== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values.
This MDN doc explains how operators are parsed concerning each other.
The increment and decrement operators in JavaScript will add one (+1) or subtract one (-1), respectively, to their operand, and then return a value. This Codeburst lesson explains that using ++ or -- prior to our variable, the operation executes and adds/subtracts 1 prior to returning.
Another useful post on Hacker Noon about prefix vs. postfix and the difference between them.
Assignment operators assign values to JavaScript variables. W3Schools provides a list of the various assignment operator that can be used.
The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already. Check out the MDN web docs for more information.