DEV Community

ajidk
ajidk

Posted on

Data Instances

When a new piece of data is introduced into a JavaScript program, the program keeps track of it in an instance of that data type. An instance is an individual case of a data type.

Booleans

Booleans are a primitive data type. They can be either true or false.

let boolean = true;
Enter fullscreen mode Exit fullscreen mode

Math.random()

The Math.random() function returns a floating-point,random number between 0 (inclusive) and 1 (exclusive).

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

Math.floor()

The Math.floor() functions returns largers less than or equal to a given number.

console.log(Math.floor(5.05)); // 5
Enter fullscreen mode Exit fullscreen mode

Single Line Comment

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

// This line will donate a comment
Enter fullscreen mode Exit fullscreen mode

Strings

String are a primitive data type. They are any grouping of characters (letters, spaces, numbers, or symbols) surrounded by single quotes' or double quotes ".

let single = 'Who am I';
let double = "Who am I";
Enter fullscreen mode Exit fullscreen mode

Null

Null is a primitive data type. It represents the intentional absence of value. In code, it is represented as null.

let x = null;
Enter fullscreen mode Exit fullscreen mode

Arithmetic Operators

JavaScript support arithmetic operators for:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % module

Multi-line Comments

In JavaScript, 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.

/*
Multi line comment must be
changed before deployment.
*/
let baseUrl = 'https://ajidk.vercel.app/'
Enter fullscreen mode Exit fullscreen mode

Remainder / Modulo Operator

The remainder operator, sometimes called modulo, returns the number that remains after the right-hand number divides into the left-hand number as many times as it evenly can.

// calculates # of weeks in a year, rounds down to
nearest integer.
const weeksInYear = Math.floor(365/7);

// calculates the number of days left over after 365 is
divides by 7
const daysLeftOver = 365 % 7

console.log("A year has " + weeksInYear + " weeks and " 
+ daysLeftOver + " days");
Enter fullscreen mode Exit fullscreen mode

String Interpolation

String interpolation is the process of evaluating string literals containing one or more placeholders (expressions, variables, etc)
It can be performed using template literals:
text $(expression) text

let age = 7

// String concatenation
'Tommy is ' + age + ' years old.'

// String interpolation
`Tommy is ${age} years old.`
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

An assignment operator assigns a value to its left operand based on the value of its right operand. Here are some of them.

  • += addition assignment
  • - subtraction assignment
  • * multiplication assignment
  • / division assignment
  • % module assignment
let number = 100

// Both statements will add 10
number = number + 10
number += 10

console.log(number) // 120
Enter fullscreen mode Exit fullscreen mode

Variables

Variables are used whenever there's a need to store a piece of data. A variable contains data that can be used in the program elsewhere. Using variables also ensures code re-usability since it can be used to replace the same value in multiple places.

const currency = '$'
let userIncome = 85000

console.log(currency + userIncome + ' is more than the average income.') // $85000 is more than the average income.
Enter fullscreen mode Exit fullscreen mode

Undefined

undefined is a primitive JavaScript value that represents lack of defined value. Variables that are declared but not initialized to a value will have the value undefined.

var a;

console.log(a) // undefined
Enter fullscreen mode Exit fullscreen mode

Template Literals

Template literals are strings that allow embedded expressions, ${expression}. While regular strings use single ' or double " quotes, template literals use backticks instead.

let name = "Ajidk"
console.log(`Hello, ${name}`) // Hello, Ajidk

console.log(`John is ${10+6} years old.`) // John is 16 years old.
Enter fullscreen mode Exit fullscreen mode

Learn JavaScript: Variables

A variable is a container for data that is stored in computer memory. It is referenced by a descriptive name that a programmer can call to assign a specific value and retrieve it.

// examples of variables
let name = "Tammy"
const found = false
var age = 3
console.log(name, found, age) // Tammy, false, 3
Enter fullscreen mode Exit fullscreen mode

Declaring Variables

To declare a variable in JavaScript, any of these 3 keywords can be used along with a variable name:

  • var is used in pre-ES6 versions of JavaScript.
  • let is the preferred way to declare a variable when it can be reassigned.
  • const is the preferred way to declare a variable with a constant value.
var age;
let weight;
const numberOfFingers = 20;
Enter fullscreen mode Exit fullscreen mode

let Keyword

let creates a local variable in JavaScript & can be-re-assigned. Initialization during the declaration of a let variable is optional. A let variable will contain undefined if nothing is assigned to it.

let count;
console.log(count) // undefined
count = 10;
console.log(count) // 10
Enter fullscreen mode Exit fullscreen mode

const Keyword

A constant variable can be declared using the keyword const.It must have an assignment. Any attempt of re-assigning a const variable will result in JavaScript runtime error.

const number = 10; 
Enter fullscreen mode Exit fullscreen mode

String Concatenation

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

let service = 'say bismillah bree'
let month = 'May 20th'
let displayText = 'oh my friends ' + service + ' so that life is blessed' + '.'

console.log(displayText) // oh my friend say bismillah bree so that life is blessed.
Enter fullscreen mode Exit fullscreen mode

Discussion (0)