JavaScript Guide: Introduction to JavaScript
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours
Console
- Theconsole is a panel which displays important messages for developers.
- Calling the
.log()method on theconsoleobject 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
- Acomment 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 orfalse 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:
1241525String 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 YorkProperties
- Aproperty 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 thelength property on the instance of a string returns the number of characters in that string.
console.log("Codecademy".length);
Output:
10Methods
- 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:
CODECADEMYBuilt-in Objects
- Built-in objects are collections of methods and properties provided byJavaScript.
Calling the.ceil() method on the built-inMath object returns the next whole number rounded up.
console.log(Math.ceil(43.8));
Output:
44'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 teamRelated articles
- Article
Different ways of reversing a string in C++
Learn multiple methods for reversing a string in C++, from basic manual approaches (like loops, recursion, stacks) to using STL (Standard Template Library) functions, and compare their performance. - Article
How to Remove Characters from a String in Python
Learn how to remove characters from a Python string using `rstrip()`, `replace()`, and `re.sub()` with examples. - Article
Why JavaScript Is Essential
Learn what is JavaScript and why it is the most popular programming language. Discover its history, web development role, backend capabilities, and future potential in technology.
Learn more on Codecademy
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours
- Take your JavaScript knowledge to the next level by learning how to use advanced functions to create more efficient programs.
- Intermediate.11 hours