Learn to Code via Tutorials on Repl.it!

← Back to all posts
Javascript Advanced Concepts Explanation
candies (153)

WARNING: Commenting will automatically update you once I updated.

Welcome to JACE!
Here youll learn everything you need to know about Javascript. Lets get started!

Statements

JavaScript statements are composed of:

Values, Operators, Expressions, Keywords, and Comments.

This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":

document.getElementById("demo").innerHTML = "Hello Dolly.";

Most JavaScript programs contain many JavaScript statements.

The statements are executed, one by one, in the same order as they are written.

Semicolons ;

Semicolons separate JavaScript statements.
Add a semicolon at the end of each executable statement:

var a, b, c;     // Declare 3 variables
a = 5;           // Assign the value 5 to a
b = 6;           // Assign the value 6 to b
c = a + b;       // Assign the sum of a and b to c

When separated by semicolons, multiple statements on one line are allowed:

a = 5; b = 6; c = a + b;

Conditional Statements

IF..Else

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.

In JavaScript we have the following conditional statements:

  1. Use if to specify a block of code to be executed, if a specified condition is true
  2. Use else to specify a block of code to be executed, if the same condition is false
  3. Use else if to specify a new condition to test, if the first condition is false
  4. Use switch to specify many alternative blocks of code to be executed
// IF Statement
if (condition) {
  //  block of code to be executed if the condition is true
}

// IF..Else Statement
if (condition1) {
  //  block of code to be executed if condition1 is true
} else (condition2) {
  //  block of code to be executed if the condition1 is false and condition2 is true
}

JavaScript Comparison and Logical Operators

Comparison and Logical operators are used to test for true or false.

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Given that x = 5, the table below explains the comparison operators:

Operator:Description:Comparing:
==equal toox == 8
===equal value and equal typex === 5
!=not equalx != 8
!==not equal value or not equal typex !== 5
>greater thanx > 8
<less thanx < 8
>=greater than or equal tox >= 8
<=less than or equal tox <= 8

How Can it be Used

Comparison operators can be used in conditional statements to compare values and take action depending on the result:

if (age < 18) text = "Too young to buy alcohol";

Logical Operators

Logical operators are used to determine the logic between variables or values.

Conditional (Ternary) Operator

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.

Syntax

variablename = (condition) ? value1:value2 

Example

var voteable = (age < 18) ? "Too young":"Old enough";

for/in Statement

Example

Loop through the properties of an object:

var person = {fname:"John", lname:"Doe", age:25};

var text = "";
var x;
for (x in person) {
  text += person[x] + " ";
}

Definition and Usage

The for/in statement loops through the properties of an object.
The block of code inside the loop will be executed once for each property.

JavaScript supports different kinds of loops:
for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - loops through a block of code once, and then repeats the loop while a specified condition is true

Note: Do not use the for/in statement to loop through arrays where index order is important. Use the for statement instead.

Syntax

for (var in object) {
  code block to be executed
}

Thanks for reading! Adding more stuff soon!

Changelog

Added for/in Statement [Section]

Comments
hotnewtop
Bookie0 (6272)

Well um this isn't thaat advanced...perhaps add functions, loops, etc.?

candies (153)

i am currently, working on pyyoutube and other stuff..

@Bookie0

candies (153)

@Bookie0 pyyoutube = download videos & get video stats