`EJS || operator not functioning as anticipated`

I am encountering an issue with my EJS code:

<link rel="stylesheet" type="text/css" href="/public/stylesheets/<%= css || 'default' %>.css" />

My expectation was that if css is undefined, it would default to

"/public/stylesheets/default.css"
. However, instead of the expected result, I am getting an error message:

css is not defined

Can someone explain if this behavior is a specific quirk of EJS, or if there might be something that I am misunderstanding?

Answer №1

In the words of VLAZ:

According to VLAZ, there is a distinction between 'css is not defined' and 'undefined'. The former indicates that it has never been declared or cannot be accessed from the current scope, while the latter implies that it has been declared but lacks a value.

I successfully resolved my issue by implementing the solution proposed by customcommander:

Instead of using

css || 'default'

which results in an error, opt for

typeof css === 'undefined' ? 'default' : css

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to append a new key to an array of objects using JavaScript

I have encountered a small issue while trying to add the key 'greeting' to an array of objects. Although my code successfully adds the key, it also adds another array at the end when I log the result. function greetDevelopers(list) { list.greet ...

The revealing module pattern in AngularJS is effective in most cases, but it can sometimes present challenges

Currently, I am engaged in a project that utilizes AngularJS. We are exploring the implementation of the revealing module pattern to ensure private functions are present in our controllers and services. While everything is functioning as anticipated, we ar ...

What is the best way to have child controllers load sequentially within ng-repeat?

Currently, I have a main controller that retrieves data containing x and y coordinates of a table (rows and columns). Each cell has a child controller responsible for preparing the values it will display based on the x and y values from the parent control ...

How can you simultaneously utilize customCell and Reorder functionalities in Kendo React Grid?

Currently, I am utilizing the Kendo React Grid with reorder functionality enabled and a custom cell for showing/hiding columns. Initially, everything works smoothly when toggling visibility without reordering. However, once I rearrange the columns and the ...

Accessing Next and Previous Elements Dynamically in TypeScript from a Dictionary or Array

I am new to Angular and currently working on an Angular 5 application. I have a task that involves retrieving the next or previous item from a dictionary (model) for navigation purposes. After researching several articles, I have devised the following solu ...

Updating AngularJS views based on window resizing

I've been working on an Angularjs application and everything is running smoothly, but I'm facing challenges when it comes to implementing a mobile version of the site. Simply using responsive styles won't cut it; I need to use different view ...

Please submit the form to log in with your credentials

Here is the HTML code snippet for a form where users can enter their username and password to log in: <form Name ="form1" Method ="POST" ACTION = "userlogin.php" id="form1"> <div id="main_body" class="full-width"> <label>User ...

The controller's AngularJS function seems to be unresponsive

Issue with AngularJs ng-click Event I'm attempting to utilize the GitHub Search-API. When a user clicks the button, my Controller should trigger a search query on GitHub. Here is my code: HTML: <head> <script src="js/AngularJS/angula ...

Maintain the header of a table at the top of the grid as you scroll

Although I know there are multiple ways to write this code, I have attempted (and fixed) it. Since I'm not well-versed in front-end development, I am finding this task to be quite challenging. I discovered that placing the table header within a table ...

The creation of a parameterized function that doubles as an object property

interface item { first: string; last: string; } const itemList = Item[]; updateAttribute = (index, attributeToUpdate) => { itemList[index].attributeToUpdate = "New first/last" } The snippet above showcases an interface named item with propertie ...

Filtering Gmaps markers using angular and javascript: A step-by-step guide

This code is designed to show markers on a map based on selected dates. Only the markers created during the chosen time period will be visible. I am monitoring a value from an external JavaScript script called obj.dateRangeSlider("values"). as shown belo ...

Retrieve the name of the product from the corresponding parent element

My goal is to trigger an alert box with the product name when the "Buy now" button is clicked. I have added the necessary code in jquery to maintain the onclick event of the button. <h2 class="product-name"><a href="product1.php" title="Sample Pr ...

Converting JSON into an HTML nested list structure

JSON: { "Item A": [ 5, { "Item A1" : 15, "Item A2" : 25, "Item A3" : 35 } ], "Item B": 45, "Item C": [ 55, { "Item C1" : 65, "Item C2" : 75, "Item C3" : 85 } ] } Desired HTML: <ul ...

Using Angular's UI-Router to handle nested states and integrating a back button functionality

I've encountered a challenge with nested state, resolve and the browser back button. Here is my states configuration: there's one abstract state and 2 children states - one for viewing a profile and another for reserving it. The standard flow in ...

Exploring JSON Object Mapping in ReactJS

Upon receiving a JSON object as a response, the console log displays the following: MyFormName: Array(5) 0: {question: "Q1", type: "star"} 1: {question: "Q1", type: "star"} 2: {question: "Q1", type: "star"} 3: {question: "Q1", type: "star"} 4: {question: ...

Gradually vanishing words while they glide across the screen

I want to achieve a similar effect seen in the game A Dark Room. The text in the game serves two purposes which I am trying to replicate: The new text is added above the old text instead of below it, pushing the older text down the page as the game progr ...

Is there a way to assign or initialize a Java array as a JavaScript array without relying on JSP?

I'm currently developing in the Play framework and I'm facing a challenge with using a Java array inside Javascript. My attempt so far is as follows - var jsarray = ${javaArray}; The javaArray refers to the array stored in the Java controller ...

Explore an array library in JavaScript

How can I access elements in a list of arrays separated by commas? [ ["service_state", "service_description", "service_icons", "svc_plugin_output", "svc_state_age", "svc_check_age", "perfometer"], ["OK", "Check_MK", "", "OK- Agent version 1.2.4p4, ...

Guide on linking a textarea with a boolean value in the child component

I have created a form component consisting of two components. The first component looks like this: <template> <div class="flex flex-col items-center justify-center gap-2"> <div class="flex w-[80%] items-center justify-ce ...

Displaying an image within a textarea using JS and CSS

Could someone help me create a form with textareas that have small images on them? I want clicking on the image to call a function fx(). Can anyone code this for me using JavaScript/jQuery/CSS? In the image below, clicking on "GO" will call Fx() I attemp ...