All technological notes.
A computer program is a list of “instructions” to be “executed” by a computer. In a programming language, these programming instructions are called statements.
A JavaScript program is a list of programming statements.
JavaScript code.JavaScript statements are composed of:
Semicolons ;
a = 5;
b = 6;
c = a + b;
White Space
Line Length and Line Breaks
Code Blocks
JavaScript statements can be grouped together in code blocks, inside curly brackets {…}.
The purpose of code blocks is to define statements to be executed together.
///* */JavaScript syntax
Case Sensitive
Character Set
The JavaScript syntax defines two types of values:
Literal: Fixed valueVariable: Variable valuesLiteral
Syntax rules for fixed values:
Variables
var, let and const to declare variables.Identifiers are JavaScript names.
Identifiers are used to name variables and keywords, and functions.
A JavaScript name must begin with:
$)_)Camel Case
// Underscore
first_name, last_name, master_card, inter_city;
// Upper Camel Case (Pascal Case)
FirstName, LastName, MasterCard, InterCity;
// Lower Camel Case
firstName, lastName, masterCard, interCity;
JavaScript uses arithmetic operators ( + - * / ) to compute values.
JavaScript uses an assignment operator ( = ) to assign values to variables:
expression
evaluation.Scope:
JavaScript has 3 types of scope:
Block scopeFunction scopeGlobal scopevar keyword can NOT have block scope.
{ } block can be accessed from outside the block.{
var x = 10;
}
console.log(x); //10
let and const provide Block Scope in JavaScript.
{ } block cannot be accessed from outside the block.{
let x = 10;
const y = 9;
}
console.log(x); // ReferenceError: x is not defined
console.log(y); // ReferenceError: y is not defined
Function Arguments
Local variables
Local variables have Function Scope.
Local variables are created when a function starts, and deleted when the function is completed.var, let and const are quite similar when declared inside a function.Function arguments (parameters) work as local variables inside functions.Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.
function myFunction() {
var carName = "Volvo"; // Function Scope
}
function myFunction() {
let carName = "Volvo"; // Function Scope
}
function myFunction() {
const carName = "Volvo"; // Function Scope
}
Global Variables
Variables declared Globally (outside any function) have Global Scope.
var, let and const are quite similar when declared outside a block.They all have Global Scope.Automatically Global
myFunction();
// code here can use carName
function myFunction() {
carName = "Volvo";
//This code example will declare a global variable carName, even if the value is assigned inside a function.
}
Global Variables in HTML
global scope is the JavaScript environment.global scope is the window object.var keyword belong to the window object.let keyword do not belong to the window objectglobal variables unless you intend to.
Hoisting
JavaScript in strict mode does not allowvariables to be used if they are not declared.
var variable can be used before declared
x = 5; // Assign 5 to x
console.log(x); //5
var x; // Declare x
Using a let variable before declared will result in a ReferenceError.
x = 5; //ReferenceError: Cannot access 'x' before initialization
let x; // Declare x
Using a const variable before declared will result in a ReferenceError.
x = 5; //SyntaxError: Missing initializer in const declaration
const x; // Declare x
use strictuse strict
Declaring Strict Mode
Declared at the beginning of a script
"use strict";
// x = 3.14; // ReferenceError: x is not defined
myFunction();
function myFunction() {
y = 3.14; // ReferenceError: y is not defined
}
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
y = 3.14; // ReferenceError: y is not defined
}
eval, arguments,with cannot be used as a variableeval() is not allowed to create variables in the scope from which it was called.this
this keyword refers to the object that called the function.undefined.code debugging
debugger Keyword
<script>
let x = 15 * 5;
debugger; // stop execution
console.log(x);
</script>