Java Script: Part-1

Js is powerful, flexible and fast programming language. it is used in web development. It provides the dynamic behavior to the website.
Fundamental of Js
Console
It is a keyword refer to an object. There are methods built into console
object one of them is .log()
method. When we write console.log()
it will print the wriiten information in paranthesis to the console.
Comment
As we write JavaScript, we can write comments in our code that the computer will ignore as our program runs. These comments exist just for human readers.
-
Single line comment: A single-line comment will comment out a single line and is denoted with two forward slashes
//
preceding it.1 2
// Prints 5 to the console console.log(5);
-
Multi-line comment A multi-line comment will comment out multiple lines and is denoted with /* to begin the comment, and */ to end the comment.
1 2 3 4 5 6
/* This is all commented console.log(10); None of this is going to run! console.log(99); */
Data types
A data type in programming refers to the classification of data that tells the compiler or interpreter what kind of value a variable holds and what operations can be performed on it. In js there are 8 fundamental data types
-
Number : Any numerical value including decimal numbers.
Ex:4
,8
,80
,60.65
-
Bigint: Any number, greater than 2^53 -1 or less than -(2^53 -1), with n appended to the number.
Ex:1234567890123456n
. -
String: a group of characters (letters, numbers,spaces,symbols etc) surrounded by single quotes
'...'
or double quotes"..."
.
Ex:hello
,world
-
Boolean: It has only two possible value
true
andfalse
without quotes. -
Null: It represent the absence of a value and it is represented by
null
keyword. -
Undefined: It is denoted by keyword
undefined
without quotes.it means that a given value does not exist. -
Symbol: Symbols are unique identifiers, useful in more complex coding.
-
Object: Objects are collection of related data.
Arithmatic Operators
It performs a task in our code. Js has several built-in arithmetic operators that allow us to perform mathematical calculations on numbers.
- Add: +
- Subtract: -
- Multiply: *
- Divide: /
- Remainder: %
|
|
String concatenation
the process of appending one string to the another is called concatenation. When a +
operator is used on two strings, it appends the right string to the left string:
|
|
Properties
All datatypes have access to specific properties that are passed down to each instance. which are created in the js code. Ex: .length
|
|
Methods
methods are actions we can perform. Datatypes have access to specific methods that allow us to handle instance of that data type.
|
|
Built in objects
In addition to console, there are other objects built into JavaScript. Ex: Math is a built in object that performs more complex mathematical operations than arithmetic.
|
|
Javascript variables
A variable is a container for a value. Informations are stored in variables. It provide a way of labeling data with a descriptive name.
Redeclare | Reassign | |
---|---|---|
var | ✅ | ✅ |
let | ❌ | ✅ |
const | ❌ | ❌ |
Create a variable using var
Before es6 version of js we could only use var keyword to declare variables. Var is short for variable. In given example Arya is stored in a variable labeled as myName.
|
|
Rules for creating a variable:
- Can’t start with number
- Variable name are case sensitive
- Cant be same as keywords
Create a variable using let
Let and const are introduced in es6. Let keyword signals that the variable can be reassigned a diff value later on.
|
|
let
keyword it automatically has a value of undefined
. We can reassign the value in let. Can not be redeclare.Create a variable using const
The const keyword was also introduced in ES6, and is short for the word constant. However, a const variable cannot be reassigned because it is constant.
|
|
Mathematical assignment operator
|
|
Increment and Decrement operator
Other mathematical assignment operators include the increment operator (++) and decrement operator (–).The increment operator will increase the value of the variable by 1. The decrement operator will decrease the value of the variable by 1. For example:
|
|
String concatenation with variable
|
|
String interpolation
In the ES6 version of JavaScript, we can insert, or interpolate, variables into strings using template literals. A template literal is wrapped by backticks `. it inc the redability of the code.
|
|
Typeof operator
If we need to check the data type of a variable’s value, we can use the typeof operator. The typeof operator checks the value to its right and returns, or passes back, a string of the data type.
|
|
Js control flow
A conditional statement checks a specific condition(s) and performs a task based on the condition(s).
If statement
In programming, we can also perform a task based on a condition using an if statement:
|
|
If…Else statements
If we wanted to add some default behavior to the if statement, we can add an else statement to run a block of code when the condition evaluates to false. An else statement must be paired with an if statement, and together they are referred to as an if…else statement.
|
|
// Prints: But the code in this block will!
Comparison operators
sometimes we need to use different types of operators to compare values. These operators are called comparison operators. Comparison operators compare the value on the left with the value on the right. Here is a list of some handy comparison operators and their syntax:
- Less than: <
- Greater than: >
- Less than or equal to: <=
- Greater than or equal to: >=
- Is equal to: ===
- Is not equal to: !==
Logical operators
Working with conditionals means that we will be using boolean true or false values. In JavaScript, there are operators that work with boolean values known as logical operators There are three logical operators:
- the and operator (&&)
- the or operator (||)
- the not operator, otherwise known as the bang operator (!)
|
|
Truthy and Falsy
The list of falsy values includes:
- 0
- Empty strings like "" or ""
- null, which represents when there is no value at all
- undefined, which represents when a declared variable lacks a value
- NaN, or Not a Number
Truthy and Falsy Assignment
|
|
Ternary operator
In the spirit of using short-hand syntax, we can use a ternary operator to simplify an if..else statement. Take a look at the if…else statement example:
|
|
We can use a ternary operator to perform the same functionality:
|
|
- The condition, isNightTime, is provided before the ?.
- Two expressions follow the ? and are separated by a colon :.
- If the condition evaluates to true, the first expression executes.
- If the condition evaluates to false, the second expression executes.
Else if statement
We can add more conditions to our if...else
with an else if
statement. The else if
statement allows for more than two possible outcomes. You can add as many else if
statements as you’d like, to make more complex conditionals!
|
|
The switch keyword
The switch
statement is used to perform different actions based on different conditions, as an alternative to multiple if...else
statements.
|
|
Functions
A function is a reusable block of code that groups together a sequence of statements to perform a specific task.
Function Declaration
In js there are many way to create a function one of them is function declaration. Just like how a variable declaration binds a value to a variable name, a function declaration binds a function to a name, or an identifier. Take a look at the anatomy of a function declaration below:
|
|
A function declaration consists of:
- The function keyword.
- The name of the function, or its identifier, followed by parentheses.
- A function body, or the block of statements required to perform a specific task, enclosed in the function’s curly brackets,
{ }
.
Calling a function
The code inside the function body will only be run when it is called. To call a function type the function name followed by parentheses.
|
|
Parameters and arguments
- Parameters allows function to accept inputs and perform task. We use parameters as placeholders for information that will be passed to the function when it is called.
- Arguments are the values that are passed to the function when it is called.Arguments can be passed to the function as values or variables.
|
|
Default parameter
Default parameters allow parameters to have a predetermined value in case no argument is passed into the function or if the argument is undefined when called.
|
|
Return
When a function is called, the computer will run through the function’s code and evaluate the result and return the result. By default, the resulting value is undefined.
|
|
Helper function
We can also use the return value of a function inside another function. These functions being called within another function are often referred to as helper functions.
|
|
function expression
Another way to define a function is to use a function expression. Here we use the function keyword. In a function expression, the function name is usually omitted. A function with no name is called an anonymous function. A function expression is often stored in a variable in order to refer to it.
|
|
arrow functions
ES6 introduced arrow function syntax, a shorter way to write functions by using the special “fat arrow” () =>
notation.
Arrow function remove the need to type out the keyword function every time we create a function. Instead, we first include the parameters inside the ( )
and then add an arrow =>
that points to the function body surrounded in { }
like this:
|
|
Concise body arrow function
JavaScript also provides several ways to refactor arrow function syntax. The most condensed form of the function is known as concise body.
-
Functions that take only a single parameter do not need that parameter to be enclosed in parentheses.
NoteHowever, if a function takes zero or multiple parameters, parentheses are required. -
A function body which has single-line block does not need curly braces. Without the curly braces, whatever that line evaluates will be automatically returned without
return
keyword. This is referred to as implicit return.
|
|
javascript scope
Scope defines where variables can be accessed or referenced. Scope is the context in which our variables are declared.
Global scope
In global scope, variables are declared outside of blocks. These variables are called global variables. Because global variables are not bound inside a block, they can be accessed by any code in the program, including code in blocks.
|
|
block scope
When a variable is defined inside a block, it is only accessible to the code within the curly braces {}
. We say that a variable has block scope. These are also known as local variables because they are only available to the code that is part of the same block.
|
|
scope pollution
Scope pollution occurs when we have too many variables in the global namespace, or when we reuse variables across different scopes.
|
|
practice good scoping