DEV Community

Joy Lee🌻
Joy Lee🌻

Posted on • Updated on

[Javscript] Introduction

Reference
Codecademy. Retrieved from https://www.codecademy.com/


JavaScript is an object-oriented programming language that powers the dynamic behavior on most websites. Alongside HTML and CSS, it is a core technology that makes the web run.


Console

The console.log() method is used to log or print messages to the console. It can also be used to print objects and other info.

console.log('Hi there!');
// Prints: Hi there!
Enter fullscreen mode Exit fullscreen mode

Comments

In JavaScript, single-line comments are created with two consecutive forward slashes //.

// I'm a Single Line Comment
Enter fullscreen mode Exit fullscreen mode

And multi-line comments are created by surrounding the lines with /* at the beginning and */ at the end. Comments are good ways for a variety of reasons like explaining a code block or indicating some hints, etc.

/*  
I'm
Multiple
Line
Comment
*/
Enter fullscreen mode Exit fullscreen mode

Data Types

Data types are the classifications we give to the different kinds of data that we use in programming. In JavaScript, there are seven fundamental data types:

  • Number: Any number, including numbers with decimals (the set of all integers and floating point numbers) : 4, 8, 1516, 23.42.
let amount = 6;
let price = 4.99;
Enter fullscreen mode Exit fullscreen mode
  • String: They are any grouping of characters (letters, spaces, numbers, or symbols) surrounded by single quotes '...' or double quotes "...".
let single = 'Wheres my bandit hat?';
let double = "Wheres my bandit hat?";
Enter fullscreen mode Exit fullscreen mode
  • Boolean: This data type only has two possible values— either true or false (without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.
let lateToWork = true;
Enter fullscreen mode Exit fullscreen mode
  • Null: This data type represents the intentional absence of a value, and is represented by the keyword null (without quotes).
let x = null;
Enter fullscreen mode Exit fullscreen mode
  • Undefined: This data type is lack of defined value. Variables that are declared but not initialized to a value will have the value undefined.
var a;
console.log(a); 
// Prints: undefined
Enter fullscreen mode Exit fullscreen mode
  • Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.

  • Object: Collections of related data.


Arithmetic Operators

JavaScript supports arithmetic operators for:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo
// Addition
5 + 5
// Subtraction
10 - 5
// Multiplication
5 * 10
// Division
10 / 5
// Modulo
10 % 5
Enter fullscreen mode Exit fullscreen mode

String Concatenation

In JavaScript, multiple strings can be concatenated together using the + operator. In the example, multiple strings and variables containing string values have been concatenated. After execution of the code block, the displayText variable will contain the concatenated string.

let service = 'credit card';
let month = 'May 30th'; 
let displayText = 'Your ' + service  + ' bill is due on ' +  month + '.';
console.log(displayText);
// Prints: Your credit card bill is due on May 30th.
Enter fullscreen mode Exit fullscreen mode

Properties

When you introduce a new piece of data into a JavaScript program, the browser saves it as an instance of the data type. Every string instance has a property called length that stores the number of characters in that string.

let message = 'good nite';
console.log(message.length);
// Prints: 9

console.log('howdy'.length);
// Prints: 5
Enter fullscreen mode Exit fullscreen mode

Libraries

Libraries contain methods that can be called by appending the library name with a period ., the method name, and a set of parentheses.

Math.random();
// ☝️ Math is the library
Enter fullscreen mode Exit fullscreen mode

Methods

Methods return information about an object, and are called by appending an instance with a period ., the method name, and parentheses. You can find a list of built-in string methods in the JavaScript documentation. Developers use documentation as a reference tool. It describes JavaScript’s keywords, methods, and syntax.

The Math.random() function returns a floating-point, random number in the range from 0 (inclusive) up to but not including 1.

console.log(Math.random());
// Prints: 0 - 0.9
Enter fullscreen mode Exit fullscreen mode

The Math.floor() function returns the largest integer less than or equal to the given number.

console.log(Math.floor(5.95)); 
// Prints: 5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Some comments have been hidden by the post's author - find out more