DEV Community

Agikonyo
Agikonyo

Posted on • Updated on

Ultimate JavaScript Guide or Introduction to Modern JavaScript

On this article will discuss the following topics on JavaScript;

  1. What is JavaScript?
  2. A Simple JavaScript Syntax
  3. Variables in JavaScript
  4. Data types in JavaScript
  5. Array methods in JavaScript
  6. Loops in JavaScript
  7. Conditional statements

Introduction into JavaScript
JavaScript is an open source and commonly known as a client-side scripting language, which is dynamic and is used to enhance the interaction of webpages. To run JavaScript You can use the web console of your preferred browser like Chrome (ctrl+shift+I on windows and ctrl+shift+k on mac ) or you can use code editors like Visual Studio code, Atom or sublime text.

Simple JavaScript Syntax
// This is my first code (This indicates a comment that gives more information about the code but is not executed)
/Welcome to my first code
Happy coding
/ (This is used in cases where we have multiple lines of comments)

Example of a simple JavaScript syntax;

Syntax

Variables in JavaScript
All JavaScript variables must be identified with unique names referred to as identifiers

How to declare variables in JavaScript
As shown below num, are variables, declared with the var keyword

a) Var

Var

b) Strings
In cases where we have, we dealing with a string value, we also declare a variable, initializing it with a string value, and then returning the value. Point to note where dealing with a string you need to surround the value with quote

String

c) Let
As shown below a, b and c are variables, declared with the let keyword

Let

d) Const
This defines a constant reference to a value. You cannot reassign a value, array or an object

Const

        **Data types in JavaScript**
Enter fullscreen mode Exit fullscreen mode

We have 7 data types in JavaScript namely; string, number, bigint, Boolean, null, undefined, and symbol. We will have a discussion on what each of them represents

i. String
A string (or a text string) is a series of characters like “Ann”. When using a string it’s supposed to be quoted with either a single quote or a double quote

String

ii. Number
JavaScript has only one type of numbers such as 1,2,3,4. In number values we can use the ones with a decimal point or not as both are allowed

Number

iii. Bigint
For a bigint, this is similar to the number data type but it holds bigger values. For this you add the letter n, next to you value to indicate that it’s an integer,

Bigint

iv. Boolean
Booleans can only have two values; True or False

Boolean

              **Array methods in JavaScript**
Enter fullscreen mode Exit fullscreen mode

An array object is used to store multiple values in a single variable

Array

JavaScript Arrays are divided into two
One-dimensional arrays- This is a linear array which consists of only one row or column
Multi-dimensional arrays- This is an array with two or more columns

             **Loops in JavaScript**
Enter fullscreen mode Exit fullscreen mode

In cases where you would want to run the same code multiple times, each time with a different value
FOR - loops through the code specified for a number of times

For_loop

WHILE - loops through a block of code for as long as the specified condition is true

While_Loop

DO WHILE – This condition loops for as long as the specified condition is true
Do_While_Loop

          **Conditional statements**
Enter fullscreen mode Exit fullscreen mode

We have four types of conditional statements in JavaScript

IF- This is when we have a block of code is executed, if the set condition is true
Syntax

if (condition) {
// code to be executed if the condition is true
}

ELSE- This is else to specify a block of code to be executed, if the same condition is false
Syntax
if (condition) {
//code to be executed if the condition is true
} else {
//code to be executed if the condition is false
}

ELSE IF- This is used else if to specify a new condition to test, if the first condition is false
Syntax
if (condition1) {
//code to be executed if condition1 is true
} else if (condition2) {
//code to be executed if the condition1 is false and condition2 is true
} else {
//code to be executed if the condition1 is false and condition2 is false
}

SWITCH- We use switch to select one of many blocks of code to be executed
Syntax
switch(expression) {
case n:
//answer block of code
break;
case n:
//answer block of code
break;
default:
default //answer block of code
}

Top comments (0)