Javascript Learning Based
Javascript Learning Based
VARIABLES TYPES
VARIABLES SCOPE
ELEMENT ENTERACTIONS
JS OPERATORS
JS ARRAY, OBJECTS, JSON
VARIABLES TYPES
Variables are used to store data values. For example, a variable may be used to store a
user’s email address, or their name. In JavaScript, a variable can contain any types of data, such
as a string, a true or false boolean, an object, or a number.
VARIABLES TYPES
var
let
const
VARIABLES TYPES
VAR
And also
VAR PROBLEMS
There's a weakness that comes with var. I'll use the example below to explain
So, since times > 3 returns true, greeter is redefined to "say Hello instead". While
this is not a problem if you knowingly want greeter to be redefined, it becomes a
problem when you do not realize that a variable greeter has already been defined
before.
If you have used greeter in other parts of your code, you might be surprised at the
output you might get. This will likely cause a lot of bugs in your code. This is why let
and const are necessary.
VARIABLES TYPES
LET
let is now preferred for variable declaration. It's
no surprise as it comes as an improvement to
var declarations. It also solves the problem
with var that we just covered. Let's consider
why this is so.
We see that using hello outside its block (the curly braces
where it was defined) returns an error. This is because let
LET CAN BE UPDATED BUT NOT RE-DECLARED. variables are block scoped
Cannot be Updated
Cannot be Re-Declared
VARIABLES SCOPE
Block scope
Function scope
Global scope
VARIABLES SCOPE
BLOCK SCOPE
Before ES6 (2015), JavaScript had only Global Scope and Function Scope.
ES6 introduced two important new JavaScript keywords: let and const.
These two keywords provide Block Scope in JavaScript.
Variables declared inside a { } block cannot be accessed from outside the block:
Variables declared with the var keyword can NOT have block scope.
Variables declared inside a { } block can be accessed from outside the block.
VARIABLES SCOPE
LOCAL SCOPE
Since JavaScript supports HTML, we can get HTML Elements by using different ways
getElementByID()
Getting element using their attribute id
querySelector()
Getting element using QUERY, means filtering element based on a certain condition of a query
Get the first element with class="example" Get the first element with id="example"
Getting the first <p> element
querySelectorAll()
We can get all of the classes of an element by selecting that element, and use classList property
ATTRIBUTES
We can get all of the classes of an element by selecting that element, and use classList property
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Conditional Operators
Type Operators
JAVASCRIPT OPERATORS
ARITHMETIC OPERATORS
An array is a special variable, which can hold more than one value:
Spaces and line breaks are not important. A declaration can span multiple lines:
The following example also creates an Array, and assigns values to it:
JS ARRAY, OBJECTS, JSON
ARRAY METHODS
Result
.join() - It behaves just like toString(), but in addition you can specify the separator:
Result
Result
Result
JS ARRAY, OBJECTS, JSON
REAL LIFE OBJECTS, PROPERTIES, AND METHODS
A car has properties like weight and color, and methods like start and stop:
All cars have the same properties, but the property values differ from car to car.
All cars have the same methods, but the methods are performed at different times.
JS ARRAY, OBJECTS, JSON
JAVASCRIPT OBJECTS
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
The values are written as name:value pairs (name and value separated by a colon).
OR
The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only.
Code for reading and generating JSON data can be written in any programming language.
JS ARRAY, OBJECTS, JSON
ARRAY AND OBJECT CONVERTION TO JSON
Since JSON is like an array and object but in a text form only, we can convert an array and objects to json