JavaScript · Viking Code School

JavaScript Basics with Viking Code School

JavaScript is a client side scripting language that has become one of the most popular programming languages to make webpages dynamic. The “Falling in Love with JavaScript” course of the advanced prep work for Viking Code School began by introducing all of the basic syntax, as well as types and operators, functions, looping, control flow, arrays, objects, and how to utilize all of these items in conjunction with one another. I have also completed Codecademy‘s JavaScript courses alongside VCS’s course.

The following are a series of questions that cover some of the topics of the Viking’s JavaScript course that I answered to review the material and test my knowledge.

An Introduction to JavaScript

How do you include JavaScript in your project?

JavaScript can be written in the HTML file between script tags or imported from a separate JavaScript (.js) file listing the source location within the script tag.

Why might you put your JavaScript includes at the bottom of the body tag?

The script tag for a JavaScript file must be placed within the head tags of the HTML or right before the closing body tag. If it is placed in the head tags, the JavaScript file should have a function of $(document).ready() at the start to ensure that the DOM is ready before running the script. If placed in the body tags, it must be placed right before the closing body tag to ensure the DOM is ready before the script runs.

What is “JS Bin“?

JS Bin is an in browser text editor that allows you to view your JavaScript source code and a console for viewing the output simultaneously. Using this tool allows a programmer to experiment with JavaScript and even save and share their code with others.

Where is the official JavaScript documentation located?

The official JavaScript documentation can be found on the Mozilla Developer Network at https://developer.mozilla.org/en-US/docs/Web/JavaScript

Basic Syntax: Types

Are semicolons required?

Yes, JavaScript requires semicolons at the end of lines.

Are curly braces required?

Yes, curly braces are required for blocks of code for functions, loops, and conditionals.

Are parentheses required for function invocation?

When calling a function, parentheses are required.

What’s the difference between using var myVar = “foo”; and myVar = “foo”;?

Using the var statement inside a function will create a local variable. If you do not use the var statement for a variable within a function it will look up the inheritance chain until it finds the variable or hits the global scope where it will create it.

What’s the difference between JS’s undefined and null?

undefined is the value given to a variable that has been declared without a value assigned to it. null is an assigned value that intentionally represents no value. undefined is a type while null is an object.

Is undefined falsy?

Yes, both undefined and null are falsy.

How do you make an alert box pop up?

alert(“exampleString”);

How do you write to the console?

console.log(“exampleString”);

What can you put as keys in an Object in JavaScript?

The keys of an object are always strings.

What can you put as values?

Values for keys can be any of the primitive data types (strings, booleans, numbers), as well as other objects or functions.

What are two ways to get or set values to or from an object?

You can get or set values to or from an object by using either bracket notation or dot notation.

//example of setting up an object with literal syntax

var country = {};

//example of setting a value to the name key of the above object using dot notation

country.name = “exampleCountryName”;

//example of setting a value to the continent key of the above object using bracket notation

country.continent = “exampleContinent”;

In an object, is there a difference between using a plain text identifier like myProperty versus a string like “myProperty”?

Text identifiers or string identifiers can be used interchangeably within objects.

How do you “cast” a String as a Number?

You can use the Number function to cast a string as a number. Example: Number(“3”) turns the string 3 into the number 3.

How do you test the type of an object?

typeof can be used before the name of an object to determine what type of object it is. You will get a result such as function, object, string, number, or boolean.

What is the type of an Array?

Arrays return as an object type.

What’s the difference between passing a function and invoking it?

Invoking a function means to call and run it using parentheses and arguments if applicable. Passing a function means to pass it around using its name without parentheses like you would another variable or object.

What are logical operators?

Logical operators include (and) && and (or) || – The && logical operator will return true if the arguments on both sides are true and false if either of them is false. The || logical operator will return true if either of its arguments is true.

What are comparison operators?

Comparison operators include ==, !=, ===, !===, >, >=, <, and <=. The simple equality operator (==) returns true if both arguments equal each other even if their types are different. The simple inequality operator (!=) returns true if both arguments do not equal each other even if their types are different. The strict equality (===) and strict (!==) inequality operators do the same, except the types of the arguments must be the same as well. The remaining comparison operators include greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=).

Basic Syntax: Looping

What’s the shortcut for incrementing an iterator like i?

i++ increases the iterator by 1, while i– decreases the iterator by 1.

How do you stop the iteration?

Iterations are stopped when the conditional to be evaluated on each iteration has been met or if a break; statement has been called in one of the subsets of the loop, such as an if statement whose conditions has been met.

How do you jump to the next iteration?

As long as the conditional to be evaluated on each iteration of the loop has not yet been met, the loop will continue. In addition, using the continue; statement in part of the loop will force the loop to jump to the beginning again, bypasses the remaining statements of that iteration.

Basic Syntax: Control Flow

How do Ternary conditionals work in JavaScript?

A ternary statement is a condensed if/else statement. A predicate is defined followed by a question mark then two results between a colon. The first will run if the predicate is true, the second will run if the predicate is false. Example:

predicate ? ifTrue : ifFalse

How do you invoke a function that’s tucked inside of an object?

You can use dot notation to access the function, such as ObjectName.functionName();

What objects are falsy in JavaScript?

false, “” (empty string), NaN (not a number), null, undefined, and 0 (the number) are all falsy in JavaScript.

Basic Syntax: Arrays

What are 2 ways to create an array?

Arrays can be created with a constructor or a literal declaration.

An array created with a constructor:

var arrayExample1 = new Array(“item1″,”item2”);

An array created with a literal declaration:

var arrayExample2 = [1, 2, 3];

How can you add or remove items to the front of an array?

Items can be added to the front of an array by using unshift and removed by using shift. Examples:

arrayName.unshift(1); //will add 1 to the 0 index of the array

arrayName.shift(); //will removed 1 from the 0 index of the array

How can you add or remove items to the back of an array?

Items can be added to the end of an array by using push and removed by using pop. Examples:

arrayName.push(1); //will add 1 to the end of the array

arrayName.pop(); //will remove 1 from the end of the array

What is returned if you try to access an array index where nothing is stored?

undefined is returned for an array index with nothing stored.

How do you combine arrays?

Arrays can be combined using the .concat(); method. Example:

[1,2].concat([3,4]); //will produce [1,2,3,4]

How do you turn an array into a string?

To turn an array into a string you can use the .join(); method. Example:

[1,2].join(); //will produce “1,2”

How do you sort an array?

To sort an array use the .sort(); method. Example:

[1,3,4,2].sort(); //will produce [1,2,3,4]

How do you compare two arrays?

Compare two arrays using functions such as descending in combination with .sort();

What does the splice method do?

The splice method allows you to remove and return a specified number of elements in a destructive manner. It also optionally lets you specify a new set of elements to insert at those same index positions.

How is forEach different from for?

The for loop will loop until the iteration conditional is met. The forEach method will loop for each of the elements of an array.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s