Introduction to JavaScript

Console

  • The console is a panel which displays important messages for developers.
  • Calling the .log() method on the console object will print or log to the console the data within parenthesis.

The string, Hello!, is being printed to the console.

console.log("Hello!");

Output:

Hello!

Comments

  • A comment is a useful note within a program.
  • It is ignored by the program and exists solely for informational purposes to those reading your code.

Use two forward slashes // for a single line comment. Use `/at the start and/` at the end of a multi-line comment.

//Hey! I'm ignored by the program.
 /* 
 Me too! 
 This is all commented.
 */

Data Types

String

  • Any grouping of characters surrounded by single '' or double "" quotes
    'New York City'
    
    Number
  • Any number including decimals
    40.7
    
    Boolean
  • true or false values
    true
    

Null

  • The absence of a value
    null
    

Arithmetic Operators

  • Addition: +

  • Subtraction: -

  • Multiplication: *

  • Division: /

  • Remainder (modulo): %

    • Returns the remainder of dividing the right-hand number by the left-hand number.
console.log(5 + 7);

console.log(6 - 2);

console.log(3 * 5);

console.log(10 / 5);

console.log(22 % 7);

Output:

12
4
15
2
5

String Concatenation

  • String concatenation is the process of appending one string to another.
  • Using the + operator on two strings, we can append the string on the right to the string on the left.
console.log('New ' + 'York');

Output:

New York

Properties

  • A property is stored information about an instance of a data type.
  • To utilize a property, append an instance with a period followed by the property name.

Appending the length property on the instance of a string returns the number of characters in that string.

console.log("Codecademy".length);

Output:

10

Methods

  • Methods are actions that can be performed on an instance of a data type.
  • To utilize a method, append an instance with a period, (the . operator), the method name, and opening and closing parentheses.

Calling the .toUpperCase()method on the string instance returns the string in all capital letters.

console.log('codecademy'.toUpperCase());

Output:

CODECADEMY

Built-in Objects

  • Built-in objects are collections of methods and properties provided by JavaScript.

Calling the .ceil() method on the built-in Math object returns the next whole number rounded up.

console.log(Math.ceil(43.8));

Output:

44
Made in NYC © 2018 Codecademy