JavaScript Flashcards
What is JavaScript
Javascript is used by programmers across the world to create dynamic and interactive web content like applications and browsers.
Semicolons ;
Semicolons separate JavaScript statements.
let a, b, c; // Declare 3 variables
a = 5; // Assign the value 5 to a
b = 6; // Assign the value 6 to b
c = a + b; // Assign the sum of a and b to c
JavaScript Values
The JavaScript syntax defines two types of values:
Fixed values
Variable values
In a programming language, variables are used to store data values.
JavaScript uses the keywords var, let and const to declare variables.
An equal sign is used to assign values to variables.
JavaScript and Camel Case
Hyphens are not allowed in JavaScript. They are reserved for subtractions. firstName, lastName, masterCard, interCity.
JavaScript Comments
//
4 Ways to Declare a JavaScript Variable
4 Ways to Declare a JavaScript Variable: Using var Using let Using const Using nothing
When to Use JavaScript const?
If you want a general rule: always declare variables with const.
If you think the value of the variable can change, use let.
The two variables price1 and price2 are declared with the const keyword.
These are constant values and cannot be changed.
The variable total is declared with the let keyword.
JavaScript Let
Variables defined with let cannot be Redeclared. let x = "John Doe";
let x = 0;
// SyntaxError: ‘x’ has already been declared
Variables defined with let must be Declared before use.
Variables defined with let have Block Scope.
Block Scope
Block Scope in JavaScript.
Variables declared inside a { } block cannot be accessed from outside the block:
{ let x = 2; } // x can NOT be used here
Variables declared with the var keyword can NOT have block scope.
Variables declared inside a { } block can be accessed from outside the block.
Block Scope and LET
Redeclaring a variable inside a block will not redeclare the variable outside the block:
let x = 10; // Here x is 10
{ let x = 2; // Here x is 2 }
// Here x is 10
JavaScript Const
Variables defined with const cannot be Redeclared.
Variables defined with const cannot be Reassigned.
Variables defined with const have Block Scope.
JavaScript const variables must be assigned a value when they are declared:
Use const when you declare:
A new Array
A new Object
A new Function
A new RegExp
Arrays with CONST
// You can create a constant array: const cars = ["Saab", "Volvo", "BMW"];
// You can change an element: cars[0] = "Toyota";
// You can add an element: cars.push("Audi");
Constant Objects
// You can create a const object: const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property: car.color = "red";
// You can add a property: car.owner = "Johnson";
3 data types
let length = 16; // Number let lastName = "Johnson"; // String let x = {firstName:"John", lastName:"Doe"}; // Object
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task.
A JavaScript function is executed when “something” invokes it (calls it).