JavaScript Guide: Introduction to JavaScript

Let's go through some basic JavaScript syntax.

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.

Single-line comment:

//Hey! I'm ignored by the program.

Multi-line comment:

/*
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

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team