Introduction to Javascript: Beginner Introduction to Javascript (All you need to Know)
This introduction to JavaScript will cover the fundamental concepts and features, providing a solid foundation for anyone looking to start their journey in coding and web development. Whether you’re aiming to build responsive websites or develop engaging user interfaces, mastering JavaScript is a crucial step in becoming a proficient web developer.
JavaScript files have the file extension .js
Keywords in JavaScript
Keywords are reserved words that have special meanings and are used to perform various operations within js
Keywords include:- await
, break
, case
, catch
, class
, const
, continue
, debugger
, default
, delete
, do
, else
, enum
, export
, extends
, false
, finally
, for
, function
, if
, import
, in
, instanceof
, new
, null
, return
, super
, switch
, this
, throw
, true
, try
, typeof
, var
, void
, while
, with
, yield
and many more.
Variables
[LAYMAN]: Container for storing values
var
,let
, orconst
Rules when declaring variables in js- Variable names can contain letters, digits, underscores (_), and dollar signs ($).
- Variable names must begin with a letter, underscore, or dollar sign.
- Variable names are case-sensitive (myVar and myvar are different variables).
- Reserved words cannot be used as variable names (e.g., var, function, let, etc.).
Data Types
Number
Numerical value
String
Represents any data inside quotation marks ""
Boolean
Yes/no; 0/1 - true/false data type
Null
Empty value
Undefined
A variable that has been declared but not yet assigned a value.
Object
Key value pair value (blueprint info)
Array
Data type for storing lists
Symbol (ES6)
Represents a unique and immutable value that can be used as the key of an object property.
BigInt (ES2020)
Allows you to represent whole numbers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with the Number type.
Primitive Data Types
Basic, immutable data types that hold the actual values and not references.
Operations
JavaScript supports a wide variety of operations that can be performed on different data types. These operations can be arithmetic, comparison, logical, bitwise, assignment, and more.
1. Arithmetic Operations
These operations are used to perform mathematical calculations.
- Addition (
+
): Adds two operands. - Subtraction (``): Subtracts the second operand from the first.
- Multiplication (``): Multiplies two operands.
- Division (
/
): Divides the first operand by the second. - Modulus (
%
): Returns the remainder of the division of the first operand by the second. - *Exponentiation (
*
)**: Raises the first operand to the power of the second operand. - Increment (
++
): Increases an operand by one. - Decrement (
-
): Decreases an operand by one.
Example:
2. Comparison Operations
These operations compare two operands and return a Boolean value (true
or false
).
- Equal (
==
): Checks if two operands are equal (type conversion is performed). - Strict Equal (
===
): Checks if two operands are equal and of the same type. - Not Equal (
!=
): Checks if two operands are not equal (type conversion is performed). - Strict Not Equal (
!==
): Checks if two operands are not equal or not of the same type. - Greater Than (
>
): Checks if the first operand is greater than the second. - Less Than (
<
): Checks if the first operand is less than the second. - Greater Than or Equal (
>=
): Checks if the first operand is greater than or equal to the second. - Less Than or Equal (
<=
): Checks if the first operand is less than or equal to the second.
Example:
3. Logical Operations
These operations are used to combine or invert Boolean values.
- Logical AND (
&&
): Returnstrue
if both operands aretrue
. - Logical OR (
||
): Returnstrue
if at least one operand istrue
. - Logical NOT (
!
): Inverts the Boolean value of the operand.
Example:
4. Bitwise Operations
These operations are performed on the binary representations of numbers.
- Bitwise AND (
&
): Performs a binary AND operation. - Bitwise OR (
|
): Performs a binary OR operation. - Bitwise XOR (
^
): Performs a binary XOR operation. - Bitwise NOT (
~
): Inverts the bits of the operand. - Left Shift (
<<
): Shifts the bits of the first operand to the left by the number of positions specified by the second operand. - Right Shift (
>>
): Shifts the bits of the first operand to the right by the number of positions specified by the second operand. - Unsigned Right Shift (
>>>
): Shifts the bits of the first operand to the right by the number of positions specified by the second operand, filling the leftmost bits with zeros.
Example:
5. Assignment Operations
These operations assign values to variables.
- Assignment (
=
): Assigns the right operand's value to the left operand. - Addition Assignment (
+=
): Adds the right operand to the left operand and assigns the result to the left operand. - Subtraction Assignment (
=
): Subtracts the right operand from the left operand and assigns the result to the left operand. - Multiplication Assignment (
=
): Multiplies the left operand by the right operand and assigns the result to the left operand. - Division Assignment (
/=
): Divides the left operand by the right operand and assigns the result to the left operand. - Modulus Assignment (
%=
): Takes the modulus of the left operand by the right operand and assigns the result to the left operand. - *Exponentiation Assignment (
*=
)**: Raises the left operand to the power of the right operand and assigns the result to the left operand. - Bitwise AND Assignment (
&=
): Performs a bitwise AND operation on the left operand and the right operand and assigns the result to the left operand. - Bitwise OR Assignment (
|=
): Performs a bitwise OR operation on the left operand and the right operand and assigns the result to the left operand. - Bitwise XOR Assignment (
^=
): Performs a bitwise XOR operation on the left operand and the right operand and assigns the result to the left operand. - Left Shift Assignment (
<<=
): Performs a left shift on the left operand by the number of positions specified by the right operand and assigns the result to the left operand. - Right Shift Assignment (
>>=
): Performs a right shift on the left operand by the number of positions specified by the right operand and assigns the result to the left operand. - Unsigned Right Shift Assignment (
>>>=
): Performs an unsigned right shift on the left operand by the number of positions specified by the right operand and assigns the result to the left operand.
Example:
6. String Operations
These operations are used to manipulate and combine strings.
- Concatenation (
+
): Combines two or more strings. - Template Literals (```): Embeds expressions within string literals using backticks.
Example:
Functions
functions are used to encapsulate a pieces of code that can be executed and reused. functions are defined using the function keyword. They can be named or anonymous.
Arrow functions provide a more concise syntax than normal functions, especially useful for short, single-expression functions.
Conditional Statements
They are used to make decisions in code based on certain conditions.
if else
Statement
The if
statement evaluates a condition inside parentheses. If the condition is true, the block of code inside the curly braces is executed. The else
statement is used with an if statement to execute a block of code if the if
condition evaluates to false.
Ternary Operator (? :
)
The ternary operator provides a concise way to write simple if...else statements. It evaluates a condition and returns one value if true, and another value if false.
switch
Statement
The switch
statement is used to perform different actions based on different conditions. It evaluates an expression and matches the expression's value to a case clause, and executes the associated block of code.
Loops
They are used to execute a block of code repeatedly until a specified condition evaluates to false. They provide a way to iterate over arrays, manipulate strings, and perform other repetitive tasks efficiently.
for
Loop
Use for
when you know the number of iterations in advance.
while
Loop & do...while
Loop
Use while
and do...while
when the number of iterations is determined by a condition.
for...in
Loop
for...in
is used to iterate over object properties.
for...of
Loop
for...of
is used to iterate over iterable objects like arrays and strings.
Arrays and Objects
Arrays and objects are fundamental data structures in JavaScript, they are used for organizing and manipulating data.
Arrays
Arrays are ordered collections of elements accessed by index, useful for lists of similar items.
Creating Arrays:
Accessing Array Elements:
Modifying Arrays:
Iterating Over Arrays:
Array Methods
indexOf() | push() | splice() | join() | concat() | toSpliced() |
---|---|---|---|---|---|
pop() | shift() | lenth() | at() | copyWithin() | |
slice() | unshift() | toString() | delete | flat() |
Objects:
Objects are collections of key-value pairs, accessed by key, ideal for structured data with different properties.
Creating Objects:
Adding and Modifying Properties:
Nested Objects:
Iterating Over Object Properties:
Events
Events are actions Events in JavaScript allow you to respond to user actions and browser events, enhancing interactivity in your web applications.
High Order Functions in JS
Higher-order functions in JavaScript accept or return functions, enabling flexible and concise coding by treating functions as values.
.map()
.filter()
Filters elements of an array based on a condition and returns a new array with the filtered elements.
.reduce()
Reduces the elements of an array to a single value, applying a function to each element
.forEach()
Executes a provided function once for each array element
.find()
Returns the first element in the array that satisfies a provided testing function.
.some()
Checks if at least one element in the array satisfies a provided testing function
.every()
Checks if all elements in the array satisfy a provided testing function.
.sort()
Sorts the elements of an array in place and returns the sorted array.
.concat()
Returns a new array comprised of the array on which it is called joined with other arrays and/or values.
.flatMap()
Maps each element using a mapping function, then flattens the result into a new array.
JS inbuilt functions
setTimeout
setTimeout
is a built-in JavaScript function that allows you to delay the execution of a function or a piece of code for a specified amount of time.
setInterval
setInterval
is a built-in JavaScript function that repeatedly calls a function or executes a code snippet with a fixed time delay between each call
conclusion
In conclusion, this introduction to JavaScript has provided a foundation by covering essential concepts like variables, functions, loops, functions, and some high order functions. While we've explored fundamental aspects necessary for creating dynamic web applications, it's important to recognize that JavaScript is a vast language with many advanced features and frameworks such as React and Node.js. Topics like object-oriented programming, closures, modern ES6+ syntax, and best practices in JavaScript development await further exploration to fully master the language and build robust, scalable applications. Embracing these deeper aspects will undoubtedly enrich your understanding and proficiency in JavaScript development.