DEV Community

Cover image for JavaScript
CadenCGS
CadenCGS

Posted on

JavaScript

Introduction

JavaScript is a less-than-complex language that lets you implement innovative features onto your webpages. Any time you see dynamic webpages, or anything interactive on a webpage, there is a very high possibility that there is JavaScript behind it. JavaScript can work to make 2D/3D designs, clocks, jukeboxes, and so much more!

JavaScript Elements

JavaScript Types

Number

A Number is the most straightforward type of JavaScript that can be written, it is simply just numbers that can be types into the code.

let x = 23;
console.log(x);
//Output = 23
Enter fullscreen mode Exit fullscreen mode
String

A String is JavaScript is seen as alphabetical characters such as "John Doe" or "The Dog". It can be seen as text, but it must between a set of quotation marks or apostraphes.

let x = "Tulips";
console.log(x);
//Output = Tulips
Enter fullscreen mode Exit fullscreen mode
Boolean

A Boolean is a type of JavaScript Value that yields as only true and false. For example "Dog" == "Cat", this returns as False.

let x = 37;
let y = 42;
let z = Boolean(x == y);
console.log(z);
//Output = "False"
Enter fullscreen mode Exit fullscreen mode
Object

An Object in JavaScript is a variable that contains multiple elements, such as weight, name, color, and other descriptive modifers.

let mrGreen = {
        firstName: "Jacob",
        lastName: "Green",
        occupation: "Entrepreneur",
        age: 45,
        description: "He has a lot of connections",
        color: "green"
    };
Enter fullscreen mode Exit fullscreen mode
Array

An Array in JavaScript is a variable That acts like a list, and can contain a series of other elements such as strings, objects, or other JavaScript compnents.

let list = ["Apple", "Orange", "Banana"];
console.log(list[2])
//Output = "Banana"
Enter fullscreen mode Exit fullscreen mode

JavaScript Comparisons

Comparisons are typically used with Boolean operators, to modify what the Boolean function does.

Not Equal To (!==)

Not Equal To is one of the two simplest statements in all of Boolean operations. It is also straightforward in meaning, meaning something is not equal to another thing.

let x = Boolean(4 !== 3);
console.log(x);
//Output = True
Enter fullscreen mode Exit fullscreen mode
Equal To (===)

Equal To is the other one of the two simplest statements in all of Boolean operations. It is also straightforward in meaning, meaning something is equal to another thing.

let x = Boolean(23 == 23);
console.log(x);
//Output = True
Enter fullscreen mode Exit fullscreen mode
Greater Than or Equal To (>=)

Greater Than or Equal To is a Boolean operator that judges not only whether one number is equal to another, but also whether it is greater than it. It is also straightforward in meaning, meaning something is greater than or equal to another thing.

let x = Boolean(37 >= 23);
let y = Boolean(23 >= 23);
let z = Boolean(18 >= 23);
console.log(x + ", " + y + ", " + z);
//Output = True, True, False
Enter fullscreen mode Exit fullscreen mode
Less Than or Equal To (<=)

Less Than or Equal To is a Boolean operator that judges not only whether one number is equal to another, but also whether it is less than it. It is also straightforward in meaning, meaning something is less than or equal to another thing.

let x = Boolean(37 <= 23);
let y = Boolean(23 <= 23);
let z = Boolean(18 <= 23);
console.log(x + ", " + y + ", " + z);
//Output = False, True, True
Enter fullscreen mode Exit fullscreen mode
Greater Than (>)

Greater Than is a Boolean operator that judges only whether one number greater than another.

let x = Boolean(37 > 23);
let y = Boolean(23 > 23);
let z = Boolean(18 > 23);
console.log(x + ", " + y + ", " + z);
//Output = True, False, False
Enter fullscreen mode Exit fullscreen mode
Less Than (<)

Less Than is a Boolean operator that judges only whether one number less than another.

let x = Boolean(37 < 23);
let y = Boolean(23 < 23);
let z = Boolean(18 < 23);
console.log(x + ", " + y + ", " + z);
//Output = False, False, True
Enter fullscreen mode Exit fullscreen mode

JavaScript Conditionals

Conditionals are typically used to execute a certain code based on a certain case of actions.

If (if (){ })

The If operator executes code simply of the conditions between the brackets is met.

let x = "dog";
if (x == "dog"){
   console.log("X is equal to dog");
}
//Output = "X is equal to dog"
Enter fullscreen mode Exit fullscreen mode
Else If(else if (){ })

The Else If operator executes code only if the conditions are met within the statement's brackets, but are not met within any previous statement's brackets.

let x = "cat";
if (x == "dog"){
   console.log("X is equal to dog");
}

else if (x == "cat"{
   console.log("X is equal to cat");
}
//Output = "X is equal to cat"
Enter fullscreen mode Exit fullscreen mode
Else (else (){ })

The Else operator executes code only if no other if or else if statements are met.

let x = "parrot";
if (x == "dog"){
   console.log("X is equal to dog");
}

else if (x == "cat"){
   console.log("X is equal to cat");
}

else (){
   console.log("X is neither a dog nor a cat");
}
//Output = "X is neither a dog nor a cat"
Enter fullscreen mode Exit fullscreen mode

_JavaScript Loops

Loops are blocks of code that are run over and over either infinitely or until a condition is met.

For (for (statement 1, statement 2, statement 3){ })

For works as a loop until at least two conditions are met. Statement one usually servers as an initial identifier, and it only run once, statement 2 is usually the statement that tells the loop to stop based on the updated value, statement 3 usually the statement that keeps being run until statement 2 is satisfied.

for (i = 0, i < 3, i++){
   console.log(i)
}
//Output = "1"
//Output = "2"
//Output = "3"
Enter fullscreen mode Exit fullscreen mode
While (while (){ })

While is simple to understand, as long as the statement within the brackets are true, the statement will keep repeating.

while (1 == 1){
   console.log("Looping...")
}
//Output = "Looping..."
//Output = "Looping..."
//Output = "Looping..."
//This will continue infinitely
Enter fullscreen mode Exit fullscreen mode

Top comments (0)